简体   繁体   English

使用LINQ to SQL获取IQueryable结果集的不同值

[英]Using LINQ to SQL to get distinct value of an IQueryable resultset

I am using LINQ to SQL (through a DBML) that I need to limit and I am having an issue. 我正在使用LINQ to SQL(通过DBML)我需要限制并且我遇到了问题。 I am using dynamic LINQ and sending in variables that are determined previously by the end user. 我正在使用动态LINQ并发送由最终用户先前确定的变量。 I have a VIEW in the db that holds (via left outer joins) data from the main db table and 5 tables connecting to it. 我在db中有一个VIEW,它保存(通过左外连接)来自主db表的数据和连接它的5个表。 Because I am (and have to be) using dynamic LINQ the result set from the BD must be .AsQueryable() thus createing an IQueryable result set. 因为我(并且必须)使用动态LINQ,所以BD的结果集必须是.AsQueryable(),因此创建了一个IQueryable结果集。 Here is an example of what it is doing: 以下是它正在做的一个例子:

Lets say the view holds car information (Make, Model, Year, Color, Milage, service records). 让我们说该视图包含汽车信息(制作,模型,年份,颜色,Milage,服务记录)。 If the user puts in that they want to see all FORD Mustangs that have been serviced this year, the result set would come back as such 如果用户输入他们想要查看今年已经服务的所有FORD野马,结果集将会返回

[uniqueID - 1][Ford][Mustang][1/1/2012](service date)
[uniqueID - 1][Ford][Mustang][2/1/2012](service date)
[uniqueID - 1][Ford][Mustang][3/1/2012](service date)
[uniqueID - 1][Ford][Mustang][4/1/2012](service date)

Which is fine, lets assume that this particular mustang has had 4 services done this year. 哪个好,我们假设这个特殊的野马今年已经完成了4项服务。

However if the user puts in to just see all black FORDs then it comes back 但是,如果用户只是看到所有黑色FORD,那么它就会回来

[uniqueID - 1][Ford][Mustang]
[uniqueID - 1][Ford][Mustang]
[uniqueID - 1][Ford][Mustang]
[uniqueID - 1][Ford][Mustang]
[uniqueID - 2][Ford][Taurus]

It is returning a row for each service, even though the service would be null and the IQueryable that is returned has a .Distinct(). 它为每个服务返回一行,即使该服务为null并且返回的IQueryable具有.Distinct()。

return data.Distinct().OrderBy(strOrderBy).Select("New(" + strItems + ")");

What I need to be able to do, is take that IQueryable and get the distinct values from it, since the variables coming in are dynamic, I am not sure how to do this. 我需要做的是获取IQueryable并从中获取不同的值,因为进来的变量是动态的,我不知道如何做到这一点。

Which ORM are you using? 您使用的是哪个ORM? Different ORMs have different behaviours, particularly if the ORM doesn't send a "distinct" call in the sql it sends to the DB. 不同的ORM具有不同的行为,特别是如果ORM不在它发送给DB的sql中发送“distinct”调用。

Is your "data" variable resolved before running that line? 在运行该行之前,您的“数据”变量是否已解决? If so, you're doing a Distinct on Linq to Objects, not Linq to SQL 如果是这样,你在Linq对象上做了一个区别,而不是Linq到SQL

From your example, the most direct thing to do is to implement an IEqualityComparer for your query with the second Distinct overload. 从您的示例中,最直接的事情是使用第二个Distinct重载为您的查询实现IEqualityComparer。 It'll look something like this: 它看起来像这样:

return data.Distinct(new YourEqualityComparer()).OrderBy(strOrderBy).Select("New(" + strItems + ")");

Do note, if you want your ordering to be done server side, you'll have to push the OrderBy call before the distinct. 请注意,如果您希望在服务器端完成订购,则必须在distinct之前推送OrderBy调用。

Here are some useful links related to this topic: 以下是与此主题相关的一些有用链接:

http://msdn.microsoft.com/en-us/library/ms132151.aspx http://blog.lavablast.com/post/2010/05/05/Lambda-IEqualityComparer3cT3e.aspx http://www.codeproject.com/Articles/94272/A-Generic-IEqualityComparer-for-Linq-Distinct http://msdn.microsoft.com/en-us/library/ms132151.aspx http://blog.lavablast.com/post/2010/05/05/Lambda-IEqualityComparer3cT3e.aspx http://www.codeproject。 COM /条/ 94272 / A-仿制的IEqualityComparer换的LINQ鲜明

I suspect the .Distinct() is being performed ont he entire row of data, and since they all have different service dates, none are duplicated, so are not eliminated by the .Distinct() . 我怀疑.Distinct()是在整行数据上执行的,因为它们都有不同的服务日期,所以没有重复,所以不会被.Distinct()消除。

You could try changing the order of the methods to: 您可以尝试将方法的顺序更改为:

return data.OrderBy(strOrderBy).Select("New(" + strItems + ")").Distinct();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM