Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use GroupBy properly in LINQ?

I have 4 tables: Post, Category, Relation and Meta

A category can contains multiple posts, and the relation between them is stored in Relation table. A post then can has many extra info that are stored in Meta table. I want to list all post with categories and extra infos, then group them by post's ID.

I have the following query

select p.ID, p.Title, t.Name, m.Key, m.Value from Post p
left join Relation r on p.ID = r.Child
left join Category c on r.Parent = c.ID
left join Meta m on p.ID = m.Object
where m.Type = 'news'
order by p.ID

and with these sample data:

Post

ID    Title

1     A

Category

ID    Name

1     Tips
2     Tricks

Meta

ID    Object  Key      Value

1       1     Key1     Value 1
2       1     Key2     Value 2

Relation

ID    Child Parent

1       1     1
2       1     2

then the result will be

PostID      Title     Category       Key      Value

  1           A         Tips         Key1     Value1
  1           A         Tips         Key2     Value2
  1           A        Tricks        Key1     Value1
  1           A        Tricks        Key2     Value2

and I expected the result to be

PostID      Title     Categories               Meta

  1           A      Tips, Tricks    Key1=Value1, Key2=Value2

I wonder if we can convert the query from SQL to LINQ to Entities with EF v4 and the result is stored in a class like this

class Result
{
  long ID,
  string Title,
  List<string> Categories,
  Dictionary<string, string> Meta
}

Any helps would be appreciated.

like image 435
ByulTaeng Avatar asked Mar 25 '11 15:03

ByulTaeng


People also ask

How does GroupBy work LINQ?

GroupBy allows you to quickly group collections of related data by specific properties on your data. The grouped data is then arranged by sub-collections of items in those groups. Note: LINQ provides variants of each method in this article that work with either IEnumerable or IQueryable .

What does LINQ GroupBy return?

GroupBy & ToLookup return a collection that has a key and an inner collection based on a key field value. The execution of GroupBy is deferred whereas that of ToLookup is immediate. A LINQ query syntax can be end with the GroupBy or Select clause.

How do you write a count query in LINQ?

Count() methodIEnumerable<string> strings = new List<string> { "first", "then", "and then", "finally" }; // Will return 4 int result = strings. Count(); NOTE: The Count() LINQ method (an extension method to IEnumerable<T> ) is slightly different from the Count property on List<T> .

What keyword can you use in the query syntax to continue query operations after grouping?

It support query syntax in both C# and VB.Net languages. As shown in example 1. You can also use into with GroupBy in C# query. The into keyword allows you to continue with the query and can perform more query operation.


1 Answers

What's the final result you expect from the query I personally prefer to write the query like

 var q = from r in Relation
         join p in Post on r.Child equals p.ID
         join t in Term on r.Parent equals t.ID
         let x = new { p.ID, p.Title, t.Name }
         group x by x.ID into g
         select g;

this way I think (not sure) the sql generated will be simpler

like image 186
Nasmi Sabeer Avatar answered Sep 20 '22 00:09

Nasmi Sabeer