linq expression vs lambda expression

Keywords: Lambda SQL

What is a Linq expression? What is a Lambda expression?
I used this for a while ago, but I didn't find a relatively simple and clear method on the Internet. Today, I sorted out the relevant knowledge and studied it carefully when I was free

public Program()
{
List<Student> allStudent = new List<Student> {
new Student("Zhang San",23),
new Student("Li Si",29),
new Student("WangTwo",25),
new Student("Zhao Liu",26)
};
//Ling expression
var stus1 = from s in allStudent
where s.Name == "WangTwo"
select new { s.Name, s.Age };
//Lambda expression
var stus2 = allStudent.Where(t => t.Name == "WangTwo").Select(t => new { t.Name, t.Age });
}

public class Student
{
public string Name { set; get; }
public int Age { set; get; }
public Student(string name, int age)
{
this.Name = name;
this.Age = age;
}
}
Lambda True ratio Linq More elegant expression
Linq ExpressionselectCan not omit
//Linq
var students1 = from t in db.Students
where t.Name == "Zhang San"
select t;
//Lambda
var students2 = db.Students
.Where(t => t.Name == "Zhang San");

Linq Expressions must be enclosed in parentheses to get the result set
//Linq
var students1 = (from t in db.Students
where t.Name == "Zhang San"
select t).ToList();
//Lambda
var students2 = db.Students
.Where(t => t.Name == "Zhang San")
.ToList();

When to use Linq?
From the above comparison, it seems that Linq is worthless. no, that's not the case.
For example, we can choose to use Linq in the following situations:
Example 1: (this example is applicable to Linq to Object and EF query without establishing a master foreign key)
The Join in Lambda needs to pass four parameter expressions. Is it a bit dizzy...

var list1 = new Dictionary<string, string> { { "1", "Zhang San" }, { "2", "Li Si" }, { "3", "Zhang San" }, { "4", "Zhang San" } };
var list2 = new Dictionary<string, string> { { "1", "Zhang San" }, { "2", "Li Si" }, { "3", "Li Si" }, { "4", "Zhang San" } };

//Linq
var obj1 = from l1 in list1
join l2 in list2
on l1.Key equals l2.Key
select new { l1, l2 };
//Lambda
var obj = list1.Join(list2, l1 => l1.Key, l2 => l2.Key, (l1, l2) => new { l1, l2 });

Example two:

//Linq
var obj1 = from l1 in list1
join l2 in list2
on l1.Key equals l2.Key
orderby l1.Key, l2.Key descending
select new { l1, l2 };
//Lambda
var obj = list1.Join(list2, l1 => l1.Key, l2 => l2.Key, (l1, l2) => new { l1, l2 })
.OrderBy(li => li.l1.Key)
.ThenByDescending(li => li.l2.Key)
.Select(t => new { t.l1, t.l2 });

linq is closer to sql

Join query (inline, left, cross)

Linq is more suitable for join queries, as mentioned above.
Next, we write inlined, leftist, cross linked Linq and corresponding Lambda code. (purpose: some people may not, and make a note for themselves here)
Inline:

var list1 = new Dictionary<string, string> { { "1", "Zhang San" }, { "2", "Li Si" }, { "3", "Zhang San" }, { "4", "Zhang San" } };
var list2 = new Dictionary<string, string> { { "1", "Zhang San" }, { "2", "Li Si" }, { "3", "Li Si" }, { "4", "Zhang San" } };
//Linq query
var ojb2 = (from l1 in list1
join l2 in list2
on l1.Key equals l2.Key
select new { l1, l2 }).ToList();
//Lambda query
var obj = list1.Join(list2, l1 => l1.Key, l2 => l2.Key, (l1, l2) => new { l1, l2 }).ToList();

League of the left-wing writers:

var list1 = new Dictionary<string, string> { { "1", "Zhang San" }, { "2", "Li Si" }, { "3", "Zhang San" }, { "4", "Zhang San" } };
var list2 = new Dictionary<string, string> { { "1", "Zhang San" }, { "2", "Li Si" }, { "3", "Li Si" }, { "4", "Zhang San" } };
//Linq query
var ojb2 = (from l1 in list1
join l2 in list2
on l1.Key equals l2.Key into list
from l2 in list.DefaultIfEmpty()
select new { l1, l2 }).ToList();
//Lambda query
var obj = list1.GroupJoin(list2, l1 => l1.Key, l2 => l2.Key, (l1, l2) => new { l1, l2=l2.FirstOrDefault() }).ToList();

Crossover:

var list1 = new Dictionary<string, string> { { "1", "Zhang San" }, { "2", "Li Si" }, { "3", "Zhang San" }, { "4", "Zhang San" } };
var list2 = new Dictionary<string, string> { { "1", "Zhang San" }, { "2", "Li Si" }, { "3", "Li Si" }, { "4", "Zhang San" } };
//Linq query
var ojb2 = (from l1 in list1
from l2 in list2
select new { l1, l2 }).ToList();
//Lambda query
var obj = list1.SelectMany(l1 => list2.Select(l2 => new { l1,l2})).ToList();

Original text: http://www.cnblogs.com/zhaopei/p/5746414.html

Posted by zggtf211 on Tue, 31 Mar 2020 03:55:12 -0700