加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C# 中的GroupBy的动态拼接问题及GroupBy<>用法介绍

发布时间:2020-12-15 05:46:09 所属栏目:百科 来源:网络整理
导读:废话不多说了,直接给大家贴代码了,具体代码如下所示: public class Person{ public string FirstName{set;get;} public string LastName{set;get;} public Person(){} public Person(string firstName,string lastName) { FirstName = firstName; LastNam

废话不多说了,直接给大家贴代码了,具体代码如下所示:

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}rn",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<>用法介绍,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读