Linq 中的分组(在多个字段上)

发布于 2024-11-03 11:15:12 字数 355 浏览 0 评论 0原文

我使用 linq 对一些数据进行分组,如下所示:

var groupedData = from row in salesTable.AsEnumerable()                   
group row by   
row.Field<string>("InvoiceNum") into grp
select grp;

我想使用一些字段重新分组 groupedData ,例如 row.Field("InvoiceNum") 、 row.Field("InvoiceLineNum") ,我不知道 linq 分组如何处理多个字段?

I am grouping some data using linq as following :

var groupedData = from row in salesTable.AsEnumerable()                   
group row by   
row.Field<string>("InvoiceNum") into grp
select grp;

I want to regroup groupedData using some fields like row.Field("InvoiceNum") , row.Field("InvoiceLineNum") , I dont know how does the linq grouping works with multiple fields ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

∞琼窗梦回ˉ 2024-11-10 11:15:12

使用匿名类型的对象进行分组。

 var groupedData = from row in salesTable.AsEnumerable()                   
                   group row by new
                   {
                        InvoiceNum = row.Field<string>("InvoiceNum"),
                        InvoiceLineNum = row.Field<string>("InvoiceLineNum")
                   }
                   into grp
                   select grp;

或使用命名类

public class InvoiceGrouping : IEquatable<InvoiceGrouping>
{
     public string InvoiceNum { get; set; }
     public string InvoiceLineNum { get; set; }

     public bool Equals( InvoiceGrouping other )
     {
         return other != null 
                && this.InvoiceNum == other.InvoiceNum
                && this.InvoiceLineNum == other.InvoiceLineNum;
     }

     public override bool Equals( object other )
     {
         return Equals( other as InvoiceGrouping );
     }

     public override int GetHashCode()
     {
         unchecked
         {
            int hash = 17;
            hash *= (this.InvoiceNum != null ? 23 + this.InvoiceNum.GetHashCode() : 1);
            hash *= (this.InvoiceLineNum != null ? 23 + this.InvoiceLineNum.GetHashCode() : 1 );
            return hash;
         }
     }
 }

 var groupedData = from row in salesTable.AsEnumerable()                   
                   group row by new InvoiceGrouping
                   {
                        InvoiceNum = row.Field<string>("InvoiceNum"),
                        InvoiceLineNum = row.Field<string>("InvoiceLineNum")
                   }
                   into grp
                   select grp;

Use an anonymously typed object for the grouping.

 var groupedData = from row in salesTable.AsEnumerable()                   
                   group row by new
                   {
                        InvoiceNum = row.Field<string>("InvoiceNum"),
                        InvoiceLineNum = row.Field<string>("InvoiceLineNum")
                   }
                   into grp
                   select grp;

or using a named class

public class InvoiceGrouping : IEquatable<InvoiceGrouping>
{
     public string InvoiceNum { get; set; }
     public string InvoiceLineNum { get; set; }

     public bool Equals( InvoiceGrouping other )
     {
         return other != null 
                && this.InvoiceNum == other.InvoiceNum
                && this.InvoiceLineNum == other.InvoiceLineNum;
     }

     public override bool Equals( object other )
     {
         return Equals( other as InvoiceGrouping );
     }

     public override int GetHashCode()
     {
         unchecked
         {
            int hash = 17;
            hash *= (this.InvoiceNum != null ? 23 + this.InvoiceNum.GetHashCode() : 1);
            hash *= (this.InvoiceLineNum != null ? 23 + this.InvoiceLineNum.GetHashCode() : 1 );
            return hash;
         }
     }
 }

 var groupedData = from row in salesTable.AsEnumerable()                   
                   group row by new InvoiceGrouping
                   {
                        InvoiceNum = row.Field<string>("InvoiceNum"),
                        InvoiceLineNum = row.Field<string>("InvoiceLineNum")
                   }
                   into grp
                   select grp;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文