LINQ - GroupBy a key of paired object, then separate the grouped object into list of 2 objects?
I have a list of objects (in pair, in one, IsApproved
is true, and the other, IsApproved
is false, i.e.:
public class Object
{
public int Id { get; set; }
public int MatchId { get; set; }
public string Name { get; set; }
public bool IsA开发者_如何学编程pproved { get; set; }
}
The list would be something like:
1 123ABC SomeName True
2 123ABC SomeElse False
3 321CBA Name2 True
4 321CBA Name3 False
5 987ASD NameName True
6 987ASD TestTest False
I would need to be able to group them by MatchId
, then separate them so that I have a new list like this:
public class Program
{
public List<PairedObject> PairedObjects { get; set; }
}
public class PairedObject
{
public Object IsApproved { get; set; }
public Object NotApproved { get; set; }
}
Any suggestion on how to do this? Thank you very much.
list.GroupBy(o => o.MatchId)
.Select(g => new PairedObject
{
IsApproved = g.First(o => o.IsApproved),
NotApproved = g.First(o => !o.IsApproved)
});
精彩评论