Linq表达式和Lambda表达式用法对比

什么是Linq表达式?什么是Lambda表达式?
前一段时间用到这个只是,在网上也没找到比较简单明了的方法,今天就整理了一下相关知识,有空了再仔细研究研究

public Program()
{
List<Student> allStudent = new List<Student> {
new Student("张三",23),
new Student("李四",29),
new Student("王二",25),
new Student("赵六",26)
};
//Ling表达式
var stus1 = from s in allStudent
where s.Name == "王二"
select new { s.Name, s.Age };
//Lanmbda表达式
var stus2 = allStudent.Where(t => t.Name == "王二").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确实比Linq表达式更加优雅
Linq表达式的select不能省略
//Linq
var students1 = from t in db.Students
where t.Name == "张三"
select t;
//Lambda
var students2 = db.Students
.Where(t => t.Name == "张三");

Linq表达式必须需要括号包裹起来才能取结果集
//Linq
var students1 = (from t in db.Students
where t.Name == "张三"
select t).ToList();
//Lambda
var students2 = db.Students
.Where(t => t.Name == "张三")
.ToList();

 

什么时候使用Linq?
通过上面的对比,好像Linq一文不值了。no,不是这样的。
比如下面几种情况我们就可以选择使用Linq:
例一:(本例适用于Linq to Object 和 没有建主外键的EF查询)
Lambda中的Join需要传四个参数表达式,是不是有点晕了。。。

var list1 = new Dictionary<string, string> { { "1", "张三" }, { "2", "李四" }, { "3", "张三" }, { "4", "张三" } };
var list2 = new Dictionary<string, string> { { "1", "张三" }, { "2", "李四" }, { "3", "李四" }, { "4", "张三" } };

//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 });

 

例二:
Lambda需要区分OrderBy、ThenBy有没有觉得麻烦

//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更多的只是为了照顾那些写惯了sql的程序员。

联接查询(内联、左联、交叉联)

关于联接查询使用Linq会更合适一些这个上面已经说了。
接下来我们写内联、左联、交叉联的Linq和对应的Lambda代码。(目的:可能有些人不会,同时在这里也给自己做个备忘)
内联:

var list1 = new Dictionary<string, string> { { "1", "张三" }, { "2", "李四" }, { "3", "张三" }, { "4", "张三" } };
var list2 = new Dictionary<string, string> { { "1", "张三" }, { "2", "李四" }, { "3", "李四" }, { "4", "张三" } };
//Linq查询
var ojb2 = (from l1 in list1
join l2 in list2
on l1.Key equals l2.Key
select new { l1, l2 }).ToList();
//Lambda查询
var obj = list1.Join(list2, l1 => l1.Key, l2 => l2.Key, (l1, l2) => new { l1, l2 }).ToList();

 

左联:

var list1 = new Dictionary<string, string> { { "1", "张三" }, { "2", "李四" }, { "3", "张三" }, { "4", "张三" } };
var list2 = new Dictionary<string, string> { { "1", "张三" }, { "2", "李四" }, { "3", "李四" }, { "4", "张三" } };
//Linq查询
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查询
var obj = list1.GroupJoin(list2, l1 => l1.Key, l2 => l2.Key, (l1, l2) => new { l1, l2=l2.FirstOrDefault() }).ToList();

 

交叉联:

var list1 = new Dictionary<string, string> { { "1", "张三" }, { "2", "李四" }, { "3", "张三" }, { "4", "张三" } };
var list2 = new Dictionary<string, string> { { "1", "张三" }, { "2", "李四" }, { "3", "李四" }, { "4", "张三" } };
//Linq查询
var ojb2 = (from l1 in list1
from l2 in list2
select new { l1, l2 }).ToList();
//Lambda查询
var obj = list1.SelectMany(l1 => list2.Select(l2 => new { l1,l2})).ToList();

 

FROM :http://www.cnblogs.com/zhaopei/p/5746414.html

posted @ 2016-09-23 10:54  小吕2016  阅读(6937)  评论(0编辑  收藏  举报