LINQ to DataSet,通过多列进行区分

发布于 2024-07-15 17:44:11 字数 230 浏览 3 评论 0原文

只是想检查是否有办法通过多列进行区分。 提前致谢!!!

顺便说一句,我发现了一个很棒的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

戏蝶舞 2024-07-22 17:44:11

好吧,你可以先进行投影:

var qry = db.Customers.Select(cust => new {cust.ID, cust.Name, cust.Region})
                    .Distinct();

或者在查询语法中:

var qry = (from cust in db.Customers
          select new {cust.ID, cust.Name, cust.Region}).Distinct();

那样做?

Well, you can do the projection first:

var qry = db.Customers.Select(cust => new {cust.ID, cust.Name, cust.Region})
                    .Distinct();

Or in query syntax:

var qry = (from cust in db.Customers
          select new {cust.ID, cust.Name, cust.Region}).Distinct();

That do?

飘然心甜 2024-07-22 17:44:11

您可以使用 Groupby 代替 Distinct,然后选择每个组的最上面的记录

如何在没有匿名类型的情况下按多个字段进行 LINQ 区分

<块引用>

从 objEntity 中的 o 返回

 按新分组 o 
                { 
                    o.Field1, 
                    o.Field2, 
                    o.Field3, 
                    o.Field4, 
                    o.Field5 
                } 进入 grp 
                选择 grp.FirstOrDefault(); 
  

这将为您提供 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

return from o in objEntity

              group o by new
              {
                  o.Field1,
                  o.Field2,
                  o.Field3,
                  o.Field4,
                  o.Field5
              } into grp
              select grp.FirstOrDefault();

This will give you the EntityObject Rather than the AnonymousType

怪我入戏太深 2024-07-22 17:44:11

通过“通过多列区分”,您真正的意思是分组依据。

当您要求不同时,这意味着您将获得所有不同的行,或者通过使用表中的所有列来获取一组。

如果您只想对列的子集进行不同的分组,请在子句中使用分组依据,指定要分组的列。 然后,选择组,因为您只需要为每个组使用一组密钥。

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.

青春如此纠结 2024-07-22 17:44:11

另一个简单的选择是创建一个不同的字符串。

var result = collection.DistinctBy(c => c.Field1 + "." + c.Field2 + "." + c.Field3);

Another easy option is to create a single distinct string.

var result = collection.DistinctBy(c => c.Field1 + "." + c.Field2 + "." + c.Field3);
旧人 2024-07-22 17:44:11

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();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文