开发者

How can I count number of objects in a list with a unique property with linq/lambda?

开发者 https://www.devze.com 2023-03-14 19:36 出处:网络
Let\'s say I have a public class MyObject { public string Property1; //other properties //other properties

Let's say I have a

public class MyObject
{
   public string Property1;
   //other properties
   //other properties
}

and then I have a

List<MyObject> objectList

Where Property1 has one value for the first few items, another value for the next few items, 开发者_开发问答etc. How can I count the distinct number of MyObjects based on Property1?


Use GroupBy.

objectList.GroupBy(o => o.Property1);

http://www.codeproject.com/articles/35667/How-to-Use-LINQ-GroupBy.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消