0

I have 2 or more Groups like below in a table:

Id  Value GroupId
1   A       1
2   B       1
3   C       2
4   D       2

Now I wanna show total relationships (total: 2*2=4 since we have 2 groups each one with 2 members) like below:

A & C 
A & D
B & C
B & D

Or with three Groups:

Id  Value GroupId
1   A       1
2   B       1
3   C       2
4   D       2
5   E       3
6   F       3

We have 2*2*2=8 relationships:

A & C & E
A & D & E
B & C & E
B & D & E
A & C & F
A & D & F
B & C & F
B & D & F

But, how can I do this via Linq Expression? I want the result to show in View(razor).

Update: My meaning is Cartesian Product of Group members in the Table.

mihai
  • 4,592
  • 3
  • 29
  • 42
Amirhossein Mehrvarzi
  • 18,024
  • 7
  • 45
  • 70
  • 1
    It's not clear what you're using "state" to mean here... do you mean selecting one entry from each group? Where is this data coming from? (It may be tricky to do in LINQ to SQL, but relatively straightforward in LINQ to Objects. – Jon Skeet Oct 26 '14 at 22:03
  • @JonSkeet State means total relationships among members based on their groups. Yes selecting one entry from each group to show with ather group members each time with separate members of each group. Data Comming from Database – Amirhossein Mehrvarzi Oct 26 '14 at 22:07
  • I strongly suggest you give an example with three groups. Your question is very unclear at the moment. – Jon Skeet Oct 27 '14 at 11:01
  • @JonSkeet I updated question with an example with 3 group members. Thanks. – Amirhossein Mehrvarzi Oct 27 '14 at 11:13

2 Answers2

1

Group the groups into groups. move the first group into result. Then for each remaining group, join them with result :

        IEnumerable<string> result;

        var groups = (from item in list
                      group item by item.GroupId into grp
                      select grp.Select(t => t.Value)).ToList();

        result = groups.First();

        groups.RemoveAt(0);

        groups.ForEach(delegate(IEnumerable<string> value)
            {
                result = (from r in result
                         from v in value
                         select r + " " + v).ToList();

            });
Raein Hashemi
  • 3,346
  • 4
  • 22
  • 33
0

I'm not sure if I understand your question correctly but it seems to me as a filtered cartesian product of elements from different group. And if so, this code could help:

var result = elements.SelectMany(x => elements, 
                                (x, y) => Tuple.Create(x, y))
                     .Where(t => t.Item1.GroupId < t.Item2.GroupId)
                     .Select(t => Tuple.Create(t.Item1.Value, t.Item2.Value))
                     .ToList();

Note: this creates some temporary objects so be aware when using with very big collections.

Konrad Kokosa
  • 16,563
  • 2
  • 36
  • 58
  • How is that going to work for more than two groups? I think the OP wants tuples of three values if there are three groups, for example. – Jon Skeet Oct 26 '14 at 22:35
  • @JonSkeet, true, we do not know that, I understand this in that way. Let's wait for the OP clarification... – Konrad Kokosa Oct 27 '14 at 09:14
  • The OP has now clarified it. – Jon Skeet Oct 27 '14 at 11:14
  • Thanks. Your code worked fine for 2 groups when i employed the necessary condition to avoid generation of the same `Tuple`s like (a,b)=(b,a). But How to figure out the solution when I don't know how many sets(groups) exist? – Amirhossein Mehrvarzi Nov 04 '14 at 23:03