这篇文章主要介绍了C# 中的GroupBy的动态拼接问题,在文章给大家提到了C# List泛型集合中的GroupBy<>用法详解,需要的朋友可以参考下
正文
C# 中的GroupBy的动态拼接问题及GroupBy<>用法介绍
废话不多说了,直接给大家贴代码了,具体代码如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public class Person { public string FirstName{ set ; get ;} public string LastName{ set ; get ;} public Person(){} public Person( string firstName, string lastName) { FirstName = firstName; LastName = lastName; } } List<Person> personList= new List<Person>(); personList.Add( new Person() { FirstName = "Mickey" , LastName = "Mouse" }); personList.Add( new Person() { FirstName = "Mickey" , LastName = "Mouse" }); personList.Add( new Person() { FirstName = "zhang" , LastName = "san" }); string columnName= "FirstName" ; var dics=personList.GroupBy(x => GetPropertyValue(x, columnName)).ToDictionary(x=>x.Key,x=>x.Count()); foreach (var dic in dics) { textBox1.AppendText( string .Format( "{0},{1}\r\n" ,dic.Key,dic.Value)); } |
ps:下面看下C# List泛型集合中的GroupBy<>用法
发表评论