Linq - Grouping where items fall in multiple groups?
Posted
by PirateKitten
on Stack Overflow
See other posts from Stack Overflow
or by PirateKitten
Published on 2010-04-28T14:03:42Z
Indexed on
2010/04/28
14:13 UTC
Read the original article
Hit count: 111
Is it possible using Linq to create a group where items fall into more than one group?
Using the following trivial example:
public class Data
{
public string ID;
public int From;
public int To;
}
And this list:
List<Data> data = new List<Data>()
{
new Data() { ID = "A", From = 1, To = 3 }, // Call this A
new Data() { ID = "B", From = 1, To = 2 }, // Call this B
new Data() { ID = "C", From = 2, To = 3 } // Call this C
};
I'd like to group by each possible integer in the ranges From and To (though instead of finding the min + max I could supply the query with the range I want, for example 1 to 3), and in each group would be a reference to the Data instance where the grouping int fits in its range.
Hard to explain, easier to show each group and the instances I'd expect in each:
[Group 1] 1 - A, B
[Group 2] 2 - A, B, C
[Group 3] 3 - A, C
Is this possible? Or must groups be mutually exclusive?
© Stack Overflow or respective owner