linq对实体不识别方法 [英] linq to entities doesn't recognize a method

查看:120
本文介绍了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));

我收到以下异常:

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.

这是什么原因?

我该如何解决? / p>

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

推荐答案

使用linq-to-entities时,您无法在查询中使用任意.NET方法。查询中使用的每个方法必须可以翻译为SQL。它不会帮助您返回 Expession&FunC< entityType,bool>> ,因为必须为数据库服务器上的每个记录评估条件。

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 Server上的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:

  • Cannonical functions with predefined mapping to SQL equivalent
  • EdmFunctions

EdmFunctions是标有 EdmFunctionAttribute 的方法将.NET函数映射到SQL对应。那些函数通常不能在普通的.NET代码中执行,因为它们什么都不做,也不会抛出异常。他们只是Linq对实体的功能占位符。可用的EdmFunction是:

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 / li>
  • System.Data.Objects.SqlClient.SqlFunctions中的SQL Server(非紧凑型)的预定义EdmFunctions

  • 自定义映射SQL函数 - Entity设计器中的导入向导允许您导入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中手动创建函数元素,而不必将 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天全站免登陆