As I understand it, LINQ corresponds to SQL, but one corresponds to the code and one corresponds to the database. The reason for using LINQ is that increasingly sophisticated programmers can basically ignore the database and focus on the code.

However, LINQ is a little different from SQL. For example, this group by.

In SQL, if there is a group by, then the select field can only contain the group content, or the statistics fields such as count, sum, avG.

But in LINQ, it’s: Group what field do you want by group field

Such as:

var q =
    from p in db.Products
    group p by p.CategoryID into g
    select g;
Copy the code

But that’s just the simplest case, toy level. In practical application, it is common for multiple tables and fields to participate in grouping:

from a in TableA
	join b in TableB on a.Id equals b.aId
	where ((b.Type == 1 || b.Type == 2 || b.Type == 5) && b.State= =1)
	group new { a.Id, b.Name,b,CreateDate } by new { a.Id, b.Name } into g
	select (new Class1 { Id = g.Key.Id, Name = g.Key.Name ?? "" });

class Class1
{
	public int Id { get; set; }
	publid string Name { get; set; }}Copy the code

Reference article:

www.cnblogs.com/aspnet2008/… \

\