111,092
社区成员




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