LINQ - 使用类型计数对对象进行分组

发布于 2024-11-19 20:07:07 字数 478 浏览 1 评论 0原文

我有一个对象集合,每个对象都由特定的“组”定义。如何编写 LINQ 查询来生成按“组”分组的每个对象的计数。

举个例子,假设我有以下课程;

public class Release 
{
     int ReleaseNumber; 

     public ReleaseDetails[] details;

}

public class ReleaseDetails
{
    string Group;

    // other details omitted
}

对于给定的版本,我希望能够产生如下输出:

Release number 1 contains the following details;
 - 17 records in Group A
 - 12 records in Group B
 - 6 records in Group C

非常感谢任何帮助。

I have a collection of objects that each defined by a particular "group". How can I write a LINQ query to produce a count of each object grouped by "group".

As an example, lets say I have the following classes;

public class Release 
{
     int ReleaseNumber; 

     public ReleaseDetails[] details;

}

public class ReleaseDetails
{
    string Group;

    // other details omitted
}

For a given release, I'd like to be able to produce output like;

Release number 1 contains the following details;
 - 17 records in Group A
 - 12 records in Group B
 - 6 records in Group C

Any help is much appreciated.

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

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

发布评论

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

评论(2

愿与i 2024-11-26 20:07:07

您可以执行类似

var q = from d in r.Details
            group d by d.Group into counts
            select new { Count = counts.Count(), Group = counts.Key };

完整示例的操作:

        Release r = new Release
        {
            ReleaseNumber = 1
            ,
            Details = new ReleaseDetails[]
            {
                new ReleaseDetails { Group = "a"},
                new ReleaseDetails { Group = "a"},
                new ReleaseDetails { Group = "b"},
                new ReleaseDetails { Group = "c"},
                new ReleaseDetails { Group = "d"},
                new ReleaseDetails { Group = "d"},
                new ReleaseDetails { Group = "e"},

            }
        };

        var q = from d in r.Details
                group d by d.Group into counts
                select new { Count = counts.Count(), Group = counts.Key };

        foreach (var count in q)
        {
            Console.WriteLine("Group {0}: {1}", count.Group, count.Count);
        }

You can do something like

var q = from d in r.Details
            group d by d.Group into counts
            select new { Count = counts.Count(), Group = counts.Key };

Full example:

        Release r = new Release
        {
            ReleaseNumber = 1
            ,
            Details = new ReleaseDetails[]
            {
                new ReleaseDetails { Group = "a"},
                new ReleaseDetails { Group = "a"},
                new ReleaseDetails { Group = "b"},
                new ReleaseDetails { Group = "c"},
                new ReleaseDetails { Group = "d"},
                new ReleaseDetails { Group = "d"},
                new ReleaseDetails { Group = "e"},

            }
        };

        var q = from d in r.Details
                group d by d.Group into counts
                select new { Count = counts.Count(), Group = counts.Key };

        foreach (var count in q)
        {
            Console.WriteLine("Group {0}: {1}", count.Group, count.Count);
        }
唔猫 2024-11-26 20:07:07

干得好。

    public class ReleaseDetails
    {
        public string Group { get; set; }
        public ReleaseDetails() {}
        public ReleaseDetails(string grp){Group = grp;}
    }

            var qry = new Release();
            qry.details = new List<ReleaseDetails>();
            qry.details.Add(new ReleaseDetails("A"));
            qry.details.Add(new ReleaseDetails("A"));
            qry.details.Add(new ReleaseDetails("B"));
            qry.details.Add(new ReleaseDetails("C"));
            qry.details.Add(new ReleaseDetails("C"));
            qry.details.Add(new ReleaseDetails("B"));


 var result = from x in qry.details
                group x by x.Group into g
                select new 
                { 
                    Count = g.Count(), 
                    Group = g.Key 
                };

//Or using Labmda
var result1 = qry.details.GroupBy(x => x.Group).Select(g => new { Count = g.Count(), Group = g.Key });

Here you go.

    public class ReleaseDetails
    {
        public string Group { get; set; }
        public ReleaseDetails() {}
        public ReleaseDetails(string grp){Group = grp;}
    }

            var qry = new Release();
            qry.details = new List<ReleaseDetails>();
            qry.details.Add(new ReleaseDetails("A"));
            qry.details.Add(new ReleaseDetails("A"));
            qry.details.Add(new ReleaseDetails("B"));
            qry.details.Add(new ReleaseDetails("C"));
            qry.details.Add(new ReleaseDetails("C"));
            qry.details.Add(new ReleaseDetails("B"));


 var result = from x in qry.details
                group x by x.Group into g
                select new 
                { 
                    Count = g.Count(), 
                    Group = g.Key 
                };

//Or using Labmda
var result1 = qry.details.GroupBy(x => x.Group).Select(g => new { Count = g.Count(), Group = g.Key });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文