I have an entity Item like next:
class Item
{
int Id;
string Name;
int GroupId;
virtual Group Group; //refers to Group table
string Size;
virtual ICollection<Set> Sets; //refers to many-to-many relation
}
So I want to group all items like next:
ICollection<IGrouping<string, Item>> list = AllItems.GroupBy(x => x.Group.Name).ToList();
ICollection<IGrouping<string, Item>> list = AllItems.GroupBy(x => x.Size).ToList();
And I want the same, but with Sets. Is it possible somehow?
To clarify. For example, Item1 contains in every set, then I want in the end of grouping by Sets next list:
Set 1
Item 1
Item 2
Set 2
Item 1
Item 3
Set 3
Item 1
Item 4
.....
Note, I need same structure ICollection<IGrouping<string, Item>>
, to pass it next in code. I was thinking maybe to create intermediate structure, which can accept both ICollection<IGrouping<string, Item>>
and Sets list containing my items?
SelectMany
+ GroupBy
with a value selector:
ICollection<IGrouping<string, Item>> list = AllItems
.SelectMany(x => x.Sets
.Select(y => new
{
Key = y,
Value = x
}))
.GroupBy(x => x.Key.Name, x => x.Value)
.ToList();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.