开发者

C# LINQ Grouping Question

I have an object which is structured as below

public MyObject{
    int SomeNumber {get;set;}
    Object SomeObject {get;set;}
    Object SomeOt开发者_Python百科herObject {get;set;}
}

Now SomeNumber is not unique, If I have an IEnumerable How can I pull out an IEnumerable<MyObject> that all have the same SomeNumber Value?

I assume I need to use GroupBy to do this? But cant seem to get it right.

Edit: I dont know what SomeNumber is.


No need for GroupBy unless you want to make groups of them.

IEnumerable<MyObject> myEnumerable ...
myEnumerable.Where(o => o.SomeNumber == value);


var myObjects = new List<MyObject>(); // fill        

var groups = from item in myObjects group item by item.SomeNumber;
// alternatively:
// var groups = myObjects.GroupBy(item => item.SomeNumber)

foreach (var g in groups)
{
    var someNumber = g.Key;
    var members = g.ToList();
}

should do.


IEnumerable<MyObject> source = something;
var grouped = source.GroupBy(s => s.SomeNumber);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜