Enumerable.GroupBy ()的问题

spmzfz 2014-05-15 08:40:55

public class GpInformation
{
public string Code
{
get;
set;
}

public string Name
{
get;
set;
}

public string HangYe
{
get;
set;
}

}
private void button15_Click(object sender, EventArgs e)
{

GpInformation[] gi = { new GpInformation{ Code ="0001", Name = "b0", HangYe = "Bank"},
new GpInformation {Code ="0002", Name = "z1", HangYe ="Zhengjuan"},
new GpInformation{Code ="0003",Name ="b1",HangYe ="Bank" },
new GpInformation{Code ="0004",Name ="b2",HangYe ="Bank"},
new GpInformation{Code ="0005",Name = "b3",HangYe ="Bank"},
new GpInformation{Code ="0006",Name ="d1",HangYe ="Dichan"},
new GpInformation{Code ="0007",Name ="z2",HangYe ="Zhengjuan"}
//....
};

List<GpInformation> Lg = new List<GpInformation>(gi);
int Count = Lg.GroupBy(a => a.HangYe).Select(a => a.Count(b => b.HangYe == a.Key)).Max();
Console.WriteLine("Count:{0}", Count); //Count:4



}


[ 我想获得Lg按HangYi进行分组后其成员最多的个数值,并且同时想获得其HangYe值,该如何编码?
注:此实例中Lg按HangYe进行分组后其成员最多的个数值为【4】,其HangYe名称为【Bank】。
...全文
376 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
蝶恋花雨 2014-05-15
  • 打赏
  • 举报
回复
不排序的代码
(from p in Lg group p by p.HangYe into g select new { g.Key, count = g.Count() })
.Select(x => new {x.Key,x.count }).FirstOrDefault();
输出:4,bank;
蝶恋花雨 2014-05-15
  • 打赏
  • 举报
回复
 var test = (from p in Lg group p by p.HangYe into g select new { g.Key, count = g.Count() }).Select(x => new {x.Key,x.count }).OrderByDescending(x=>x.count).FirstOrDefault();
输出:4,bank;
全栈极简 2014-05-15
  • 打赏
  • 举报
回复
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            GpInformation[] gi = { new GpInformation{ Code ="0001", Name = "b0", HangYe = "Bank"},
                           new GpInformation {Code ="0002", Name = "z1", HangYe ="Zhengjuan"},
                           new GpInformation{Code ="0003",Name ="b1",HangYe ="Bank" },
                           new GpInformation{Code ="0004",Name ="b2",HangYe ="Bank"},
                           new GpInformation{Code ="0005",Name = "b3",HangYe ="Bank"},
                           new GpInformation{Code ="0006",Name ="d1",HangYe ="Dichan"},
                           new GpInformation{Code ="0007",Name ="z2",HangYe ="Zhengjuan"}
                           //....
                         };

            List<GpInformation> Lg = new List<GpInformation>(gi);

            var result = Lg.GroupBy(x => x.HangYe).Select(x => new { Key = x.Key, Count = x.Count() }).OrderByDescending(x => x.Count).FirstOrDefault();

            Console.WriteLine("Key:{0}\tCount:{1}", result.Key, result.Count);
        }
    }

    public class GpInformation
    {
        public string Code
        {
            get;
            set;
        }

        public string Name
        {
            get;
            set;
        }

        public string HangYe
        {
            get;
            set;
        }
    }
}
  • 打赏
  • 举报
回复

 var temp1 = (from p in Lg 
                       group p by p.HangYe
                       into temp
                       select new {
                         temp.Key,
                         count = temp.Count(t=>t.HangYe==temp.Key)
                       }).FirstOrDefault();
spmzfz 2014-05-15
  • 打赏
  • 举报
回复
问题已经解决:

           var V =  GpInformationsByShAndSz.GroupBy(a => a.HangYe).Select(a => new { HangYe = a.Key, Count = a.Count() }).OrderByDescending(a => a.Count).First();
           Console.WriteLine(V.HangYe + "  " + V.Count);

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧