Linq表达式

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();
复制代码

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值