C# 中的GroupBy的動(dòng)態(tài)拼接問題及GroupBy<>用法介紹
廢話不多說了,直接給大家貼代碼了,具體代碼如下所示:
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<>用法
//根據(jù)子項(xiàng)目id得到flowjump實(shí)體類
flowJumps = this.FlowJumps;
//按工序groupby flowjumps
IEnumerable<IGrouping<int, FlowJump>> query =
flowJumps.GroupBy(pet => pet.processID, pet => pet);
foreach (IGrouping<int, FlowJump> info in query)
{
List<FlowJump> sl = info.ToList<FlowJump>();//分組后的集合
//也可循環(huán)得到分組后,集合中的對(duì)象,你可以用info.Key去控制
//foreach (FlowJump set in info)
//{
//}
}
在使用的時(shí)候需要使用分組中的鍵:
var groupInfo = orderinfo.Info.GroupBy(m => m.xx).ToList();
foreach (var item in groupInfo)
{
string infotemp = item.Key;
}
總結(jié)
以上所述是小編給大家介紹的C# 中的GroupBy的動(dòng)態(tài)拼接問題及GroupBy<>用法介紹,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
C#判斷一個(gè)字符串是否包含另一個(gè)字符串的方法
這篇文章主要介紹了C#判斷一個(gè)字符串是否包含另一個(gè)字符串的方法,涉及C#中IndexOf方法的使用技巧,非常簡單實(shí)用,需要的朋友可以參考下2015-04-04
詳解C# WinForm如何實(shí)現(xiàn)自動(dòng)更新程序
C#數(shù)據(jù)結(jié)構(gòu)與算法揭秘二 線性結(jié)構(gòu)
Revit API取得變量的內(nèi)參名稱實(shí)例代碼

