C# 中的GroupBy的动态拼接问题及GroupBy<>用法介绍
程序员文章站
2023-12-19 11:27:40
废话不多说了,直接给大家贴代码了,具体代码如下所示:
public class person
{
public string firstname{set;g...
废话不多说了,直接给大家贴代码了,具体代码如下所示:
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<>用法
//根据子项目id得到flowjump实体类 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>();//分组后的集合 //也可循环得到分组后,集合中的对象,你可以用info.key去控制 //foreach (flowjump set in info) //{ //} }
在使用的时候需要使用分组中的键:
var groupinfo = orderinfo.info.groupby(m => m.xx).tolist(); foreach (var item in groupinfo) { string infotemp = item.key; }
总结
以上所述是小编给大家介绍的c# 中的groupby的动态拼接问题及groupby<>用法介绍,希望对大家有所帮助