LINQ to DataSet,通过多列进行区分
只是想检查是否有办法通过多列进行区分。 提前致谢!!!
顺便说一句,我发现了一个很棒的 LINQ 扩展 此处,但需要一些指导才能将其用于多列
Just wanted to check if there is way to do distinct by multiple columns. Thanks in advance!!!
BTW, I found a great LINQ extension here but need some guidance to use it for multiple columns
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好吧,你可以先进行投影:
或者在查询语法中:
那样做?
Well, you can do the projection first:
Or in query syntax:
That do?
您可以使用 Groupby 代替 Distinct,然后选择每个组的最上面的记录
如何在没有匿名类型的情况下按多个字段进行 LINQ 区分
这将为您提供 EntityObject 而不是 AnonymousType
Instead of Distinct you can use Groupby and then selecting the Top Most record of each group
How to LINQ Distinct by Multiple Fields without anonymous types
This will give you the EntityObject Rather than the AnonymousType
通过“通过多列区分”,您真正的意思是分组依据。
当您要求不同时,这意味着您将获得所有不同的行,或者通过使用表中的所有列来获取一组。
如果您只想对列的子集进行不同的分组,请在子句中使用分组依据,指定要分组的列。 然后,选择组,因为您只需要为每个组使用一组密钥。
By "distinct by multiple columns" what you really mean is a group by.
When you ask for distinct, it means that you are getting ALL the distinct rows, or, a group by using all the columns in the table.
If you want to only get distinct groupings for a subset of the columns, then use a group by in your clause, specifying the columns to group by. Then, select the groups, as you only want one set of keys for each group.
另一个简单的选择是创建一个不同的字符串。
Another easy option is to create a single distinct string.
var qry =(来自 db.Customers 中的 cust
select new {cust.ID, cust.Name, cust.Region}).GroupBy(x => new { x.Name,x.Region}).select(z => z.OrderBy(i => i .cust).FirstOrDefault()).ToList();
var qry = (from cust in db.Customers
select new {cust.ID, cust.Name, cust.Region}).GroupBy(x => new { x.Name,x.Region}).select(z => z.OrderBy(i => i.cust).FirstOrDefault()).ToList();