Linq Distinct(IEqualityComparer) [英] Linq Distinct(IEqualityComparer)

查看:97
本文介绍了Linq Distinct(IEqualityComparer)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!


我遇到了带有IEqualityComparer的Distinct扩展方法重载问题。



我的意思是获得resultCount>的最后五次搜索0,但避免在五个中重复。



我得到运行时NotSupportedException消息说明:用于查询运算符的不支持的重载'Distinct "。我已经成功尝试了.AsEnumerable和.ToList,但是这两个首先得到整个表,然后在内存中显示Disctinct,这是不可接受的。



我的代码如下所示:






Code Snippet

解决方案

我或多或少地尝试做同样的事情。除了在我的情况下,我试图扩展DynamicQuery API以允许在数据库上选择不同的记录。

我创建了一个扩展函数,允许无参数不同,但我正在努力如何使它与iequalitycomparer重载一起工作。

无参数重载如下所示:

< Extension()> _
Public Function Distinct(ByVal source As IQueryable)作为IQueryable
If(source is Nothing)然后抛出新的ArgumentNullException(" source")
返回source.Provider.CreateQuery(_
Expression。调用(_
GetType(可查询),"Distinct",_
New Type(){source.ElementType},source.Expression))



老实说,然而,我在黑暗中四处奔波。纯粹的运气让我碰巧遇到了上述情况。

到目前为止,我有:

< Extension()> _
Public Function Distinct(ByVal source As IQueryable,ByVal keySelector As String,ByVal elementSelector As String)As IQueryable
If(source Is Nothing)Then Throw New ArgumentNullException(" source")
If(keySelector)是没有)然后抛出新的ArgumentNullException(" keySelector")
Dim keyLambda As LambdaExpression = DynamicExpression.ParseLambda(source.ElementType,Nothing,keySelector)
Dim elementLambda As LambdaExpression = DynamicExpression.ParseLambda(source.ElementType,Nothing ,elementSelector)
返回source.Provider.CreateQuery(_
Expression.Call(_
GetType(可查询),"Distinct",_
New Type(){source.ElementType,keyLambda。 Body.Type},_
source.Exp ression,Expression.Quote(keyLambda)))



任何真正了解dynamicQuery API如何工作的人都知道如何使iequalitycomparer在重载扩展中工作吗? >结果,谢谢,结果,杰罗姆结果

Hello!

I have trouble with the Distinct extended method overload that takes a IEqualityComparer.

 

I'm mean to get the five last searches where the resultCount > 0, but avoid duplicates within the five.

 

I get the runtime NotSupportedException message stating: Unsupported overload used for query operator 'Distinct'. I have successfully tried .AsEnumerable and .ToList, but those two gets the entire table first and then makes an in memory Disctinct, which is not acceptable.

 

My code looks like this:

Code Snippet

解决方案

I'm trying, more or less, to do the same thing.  Except that in my case I am trying to extend the DynamicQuery API to allow for selecting distinct records on the database.

I've created an extension function that allows for a parameterless distinct, but I'm struggling with how to make it work with the iequalitycomparer overload.

The parameterless overload looks like this:

 <Extension()> _
       Public Function Distinct(ByVal source As IQueryable) As IQueryable
            If (source Is Nothing) Then Throw New ArgumentNullException("source")
            Return source.Provider.CreateQuery( _
                Expression.Call( _
                    GetType(Queryable), "Distinct", _
                    New Type() {source.ElementType}, source.Expression))
        End Function

To be honest, however, I'm casting around in the dark.  I happened upon the above by sheer luck. 

So far, I have:

 <Extension()> _
        Public Function Distinct(ByVal source As IQueryable, ByVal keySelector As String, ByVal elementSelector As String) As IQueryable
            If (source Is Nothing) Then Throw New ArgumentNullException("source")
            If (keySelector Is Nothing) Then Throw New ArgumentNullException("keySelector")
            Dim keyLambda As LambdaExpression = DynamicExpression.ParseLambda(source.ElementType, Nothing, keySelector)
            Dim elementLambda As LambdaExpression = DynamicExpression.ParseLambda(source.ElementType, Nothing, elementSelector)
            Return source.Provider.CreateQuery( _
                Expression.Call( _
                    GetType(Queryable), "Distinct", _
                    New Type() {source.ElementType, keyLambda.Body.Type}, _
                    source.Expression, Expression.Quote(keyLambda)))
        End Function


Does anyone who really understands how the dynamicQuery API works have an idea how to make the iequalitycomparer work in an overloaded extension?

Thanks,
Jerome


这篇关于Linq Distinct(IEqualityComparer)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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