0

I have a view in SQL server that is an aggregation of a many to many. So the data returned is something like this:

User1 : Data1
User1 : Data2
User1 : Data3
User2 : Data1
User2 : Data2

etc.

Normally if I'm using LINQ with Entity Framework I use the navigation properties and get nested lists. So I can loop through Users and a nested loop through the data from the join table.

With a flat view, it is just one big list. I'm trying to group on the user so I can use the same foreach loops to display data.

E.g.,

User1 - Data1, Data2, Data3
User2 - Data1, Data2, Data3

Is there a way to create the grouping this way based on a single table (view)

Hope this makes sense.

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
Jeff
  • 905
  • 2
  • 8
  • 17

1 Answers1

0

Something like this should do it.

var q = from x in db.tbl
        group x by x.User into g
        select new
        {
           User = g.Key,
           Data = g.Select(y => y.Data)
        };
Magnus
  • 45,362
  • 8
  • 80
  • 118
  • What type is q expected to be? I'm trying to return this as a model to a razor view in MVC3. – Jeff Sep 10 '13 at 18:23
  • So it looks like this works well. http://stackoverflow.com/questions/1160420/how-do-i-group-data-in-an-asp-net-mvc-view – Jeff Sep 10 '13 at 18:45
  • Right now it is returning an anonymous type but you can create any type you like. – Magnus Sep 10 '13 at 18:52