LINQ到实体无​​法识别的方法 [英] linq to entities doesn't recognize a method

查看:182
本文介绍了LINQ到实体无​​法识别的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有那些方法:

   public int count(
        Guid companyId, Expression<Func<T, bool>> isMatch)
    {
        var filters = new Expression<Func<T, bool>>[]{
            x => x.PriceDefinition.CompanyId == companyId,
            isMatch
        };

        return GetCount(filters);
    }

public virtual int GetCount(
            IEnumerable<Expression<Func<T, bool>>> filters)
        {
            IQueryable<T> _query = ObjectSet;

            if (filters != null)
            {
                foreach (var filter in filters)
                {
                    _query = _query.Where(filter);
                }
            }

           return _query.Count();
        }

在使用:

count(some_guid, x => x.IsMatch(entityId, inviterId, routeId, luggageTypeId));

我得到以下异常:

I get the following exception:

LINQ to Entities does not recognize the method 'Boolean IsMatch(System.Nullable`1[System.Int64], System.Nullable`1[System.Int64], System.Nullable`1[System.Int64], System.Nullable`1[System.Int64])' method, and this method cannot be translated into a store expression.

,这是什么原因呢?
我该如何解决呢?

What is the reason for this?
How can I solve it?

推荐答案

在使用LINQ到实体则不能使用查询任意.NET方法。在查询中使用的每个方法必须翻译为SQL。它不会帮你返回 Expession&LT; Func键&LT;的EntityType,布尔&GT;&GT; ,因为那里条件必须在数据库服务器上的每个记录进行评估。

When using linq-to-entities you cannot use arbitrary .NET methods in query. Each method used in the query must be translatable to SQL. It will not help you to return Expession<Func<entityType, bool>> because where condition must be evaluated for each record on the database server.

有关EF您的code是指这样的:

For EF your code means something like:

SELECT COUNT(*)
FROM ...
LEFT JOIN ...
WHERE IsMatch(....) 

由于EF验证传递给它会抛出异常,因为它不知道IsMatch等价的SQL服务器上的查询功能名称。

Because EF validates function names passed to the query it will throw exception because it doesn't know IsMatch equivalent on SQL server.

,可用于在LINQ到实体的唯一可能的功能是:

The only possible functions which can be used in Linq-to-entities are:

  • Cannonical functions with predefined mapping to SQL equivalent
  • EdmFunctions

EdmFunctions标有 EdmFunctionAttribute 这.NET函数映射到SQL对应的方法。这些功能通常无法在普通的.NET code被执行,因为他们什么都不做,或抛出异常。他们是唯一的功能占位LINQ到实体。可用EdmFunctions是:

EdmFunctions are methods marked with EdmFunctionAttribute which maps .NET function to SQL counterpart. Those functions usually cannot be executed in common .NET code because they do nothing or throw exception. They are only function place holder for Linq-to-entities. Available EdmFunctions are:

  • $ P $在 System.Data.Objects.EntityFunctions pdefined EdmFunctions
  • predefined EdmFunctions为SQL Server(不紧凑)的 System.Data.Objects.SqlClient.SqlFunctions
  • 在自定义映射SQL函数 - 在实体设计导入向导,可以导入SQL函数(除表值函数)。你可以在那之后编写自定义静态.NET函数和 EdmFunction 地图属性导入到设计师的SQL函数。
  • 在自定义模式定义的功能 - 这是EDMX文件中手动编写特殊功能(如打开XML)。这是自定义实体SQL的可重复使用的部分。
  • Predefined EdmFunctions in System.Data.Objects.EntityFunctions
  • Predefined EdmFunctions for SQL Server (not compact) in System.Data.Objects.SqlClient.SqlFunctions
  • Custom mapped SQL functions - import wizard in Entity designer allows you import SQL functions (except table valued functions). You can after that write custom static .NET function and map it by EdmFunction attribute to the SQL function imported to designer.
  • Custom model defined functions - this is special function written manually in EDMX file (opened as XML). It is custom reusable part of Entity SQL.

我已经描述<一href="http://stackoverflow.com/questions/5612827/entity-framework-where-do-i-extend-the-csdl-msl/5613642#5613642">how在另一个答案创建自定义模型功能。创建映射 SQL函数是pretty的类似。除了手动创建的功能元素EDMX您将地图 EdmFunctionAttribute 属性导入SQL函数。

I have already described how to create model defined function in another answer. Creating mapped SQL function is pretty similar. Instead of manually creating Function element in EDMX you will map EdmFunctionAttribute properties to imported SQL function.

这篇关于LINQ到实体无​​法识别的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆