资中做网站多少钱,wordpress修改代码后,手表网站 海马300米潜水表,网页美工设计夏霍什么是Linq表达式#xff1f;什么是Lambda表达式#xff1f;前一段时间用到这个只是#xff0c;在网上也没找到比较简单明了的方法#xff0c;今天就整理了一下相关知识#xff0c;有空了再仔细研究研究 public Program()
{
ListStudent allStudent new List…什么是Linq表达式什么是Lambda表达式前一段时间用到这个只是在网上也没找到比较简单明了的方法今天就整理了一下相关知识有空了再仔细研究研究 public Program()
{
ListStudent allStudent new ListStudent {
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 Dictionarystring, string { { 1, 张三 }, { 2, 李四 }, { 3, 张三 }, { 4, 张三 } };
var list2 new Dictionarystring, 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 Dictionarystring, string { { 1, 张三 }, { 2, 李四 }, { 3, 张三 }, { 4, 张三 } };
var list2 new Dictionarystring, 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 Dictionarystring, string { { 1, 张三 }, { 2, 李四 }, { 3, 张三 }, { 4, 张三 } };
var list2 new Dictionarystring, 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, l2l2.FirstOrDefault() }).ToList(); 交叉联 var list1 new Dictionarystring, string { { 1, 张三 }, { 2, 李四 }, { 3, 张三 }, { 4, 张三 } };
var list2 new Dictionarystring, 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转载于:https://www.cnblogs.com/chunhui212/p/5899158.html