0

I have got the following SQL-Table:

---------------------------------------------------
| ID | Line1    | Line2  | Line3  | Line4 | Line5 |
---------------------------------------------------
| 1  | Software | Citrix | XenApp | Null  | Null  |
---------------------------------------------------
| 2  | Software | Citrix | XenApp | Null  | Null  |
---------------------------------------------------

I used this code in order to group it by Line3:

var KategorienLine3 = result.GroupBy(x => x.Line3).ToList();

In which result represents the list including the 2 entries. Now this grouping results in this output:

[0] -> Key = XenApp
[1] -> Key = XenApp

But I don't have access to Line2. I would like to include it in the result. How can I do that, so that I have access to that as well? It don't want to group by it!! I just want to have it in the result.

Thats what it gives me after the grouping. I want to include Line2 as well. enter image description here

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
Tom el Safadi
  • 6,164
  • 5
  • 49
  • 102

1 Answers1

3

The data is there. It is just in the IGrouping<TKey, TResult> object returned by the GroupBy. The reason you don't have access to Line2 is that each grouping contains a collection of records that are of that group - and each record there is of your object's type, and has the Line2 property.

To retrieve it project the data as you want it to show:

// method syntax
var result  = data.GroupBy(key => key.Line3, item => item.Line2)
                  .Select(g => new
                  {
                      g.Key,
                      Line2 = g.ToList()
                  }).ToList();

// query syntax
var result = from item in data
             group item.Line2 by item.Line3 into g
             select new
             {
                 g.Key,
                 Line2 = g.ToList()
             };
Gilad Green
  • 36,708
  • 7
  • 61
  • 95