linq to entity 无法识别方法 [英] linq to entities doesn't recognize a method

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

问题描述

我有这些方法:

   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));

我收到以下异常:

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-to-entities 时,您不能在查询中使用任意 .NET 方法.查询中使用的每个方法都必须可转换为 SQL.返回 Expession> 对您没有帮助,因为必须为数据库服务器上的每条记录评估 where 条件.

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,您的代码意味着:

For EF your code means something like:

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

因为 EF 验证传递给查询的函数名称,它会抛出异常,因为它不知道 SQL 服务器上的 IsMatch 等效项.

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

可以在 Linq-to-entities 中使用的唯一可能的函数是:

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

  • 规范函数,具有到 SQL 等价物的预定义映射
  • EdmFunctions
  • Cannonical functions with predefined mapping to SQL equivalent
  • EdmFunctions

EdmFunctions 是用 EdmFunctionAttribute 标记的方法,它将 .NET 函数映射到 SQL 对应物.这些函数通常不能在普通的 .NET 代码中执行,因为它们什么都不做或抛出异常.它们只是 Linq-to-entities 的函数占位符.可用的 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:

  • System.Data.Objects.EntityFunctions
  • 中预定义的 EdmFunctions
  • System.Data.Objects.SqlClient.SqlFunctions
  • 中为 SQL Server 预定义的 EdmFunctions(非压缩)
  • 自定义映射 SQL 函数 - 实体设计器中的导入向导允许您导入 SQL 函数(表值函数除外).之后,您可以编写自定义静态 .NET 函数并通过 EdmFunction 属性将其映射到导入到设计器的 SQL 函数.
  • 自定义模型定义函数 - 这是在 EDMX 文件(以 XML 格式打开)中手动编写的特殊函数.它是 Entity 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.

我已经描述了如何创建另一个答案中的模型定义函数.创建映射SQL 函数非常相似.您无需在 EDMX 中手动创建 Function 元素,而是将 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 to entity 无法识别方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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