在LINQ to Entities中使用自定义方法/扩展方法的解决方法 [英] Workarounds for using custom methods/extension methods in LINQ to Entities

查看:788
本文介绍了在LINQ to Entities中使用自定义方法/扩展方法的解决方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了一个执行数据库交互的GenericRepository类。

I have defined a GenericRepository class which does the db interaction.

 protected GenericRepository rep = new GenericRepository();

在我的BLL类中,我可以查询db:

And in my BLL classes, I can query the db like:

public List<Album> GetVisibleAlbums(int accessLevel)
{
    return rep.Find<Album>(a => a.AccessLevel.BinaryAnd(accessLevel)).ToList();  
}  

BinaryAnd是一种逐位检查两个int值的扩展方法。例如 AccessLevel = 5 => AccessLevel.BinaryAnd(5) AccessLevel.binaryAnd 两者都返回true。

BinaryAnd is an extension method which checks two int values bit by bit. e.g. AccessLevel=5 => AccessLevel.BinaryAnd(5) and AccessLevel.binaryAnd(1) both return true.

但是我不能在我的LINQ查询中使用这个扩展方法。我得到一个运行时错误如下:

LINQ to Entities不能识别方法的Boolean BinaryAnd(System.Object,System.Object)方法,这个方法不能翻译into a store expression。

However I cannot use this extension method in my LINQ queries. I get a runtime error as follows:
LINQ to Entities does not recognize the method 'Boolean BinaryAnd(System.Object, System.Object)' method, and this method cannot be translated into a store expression.

也尝试将其更改为自定义方法,但没有运气。解决方法是什么?

Also tried changing it to a custom method but no luck. What are the workarounds?

我应该获得所有的相册,然后通过foreach循环迭代它们,并选择与AccessLevels匹配的那些?

Should I get all the albums and then iterate them through a foreach loop and pick those which match the AccessLevels?

推荐答案

您只能使用核心扩展方法和CLR方法当您在 IQueryable< T> 上使用Entity Framework和查询时,您的EF提供程序。这是因为查询直接转换为SQL代码并在服务器上运行。

You can only use the core extension methods and CLR methods defined for your EF provider when using Entity Framework and queries on IQueryable<T>. This is because the query is translated directly to SQL code and run on the server.

您可以流式传输整个集合(使用 .ToEnumerable )然后在本地查询,或将此转换为可由您的提供者直接翻译为SQL的方法。

You can stream the entire collection (using .ToEnumerable()) then query this locally, or convert this to a method that is translatable directly to SQL by your provider.

href =http://msdn.microsoft.com/en-us/library/bb738681.aspx>支持基本的按位操作:

That being said, basic bitwise operations are supported:


当操作数是数字类型时,按位AND,OR,NOT和XOR运算符也映射到规范函数。

The bitwise AND, OR, NOT, and XOR operators are also mapped to canonical functions when the operand is a numeric type.

所以,如果你重写这个不使用一个方法,并直接对值直接进行位操作,它应该根据需要工作。尝试类似以下内容:

So, if you rewrite this to not use a method, and just do the bitwise operation on the value directly, it should work as needed. Try something like the following:

public List<Album> GetVisibleAlbums(int accessLevel)
{
    return rep.Find<Album>(a => (a.AccessLevel & accessLevel > 0)).ToList();  
}

(我不确定当前的扩展方法是如何工作的 - 会检查看到任何标志是否回来,这似乎与您的声明匹配...)

(I'm not sure exactly how your current extension method works - the above would check to see if any of the flags come back true, which seems to match your statement...)

这篇关于在LINQ to Entities中使用自定义方法/扩展方法的解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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