在这里使用List<T> TargetList=作为要查询的集合
1.使用LINQ查询集合中满足条件的值 表达式写法
var res = from m in TargetList
where 条件&&条件
select m;
2.扩展方法的写法
var res=TargetList.Where(Mothod);
var res=TargetList.Where((temp)=>{return (条件));//lambad表达式写法
bool Mothod(T temp)
{
if(条件)
return true;
return false
}
3.联合查询 联合查询 就是会把查询的两个集合里面满足条件的每一个记录合并成新的记录
List<T> targetListT; List<K> targetListK;
var res = from m in masterList
from k in kongfuList
where m.KongFu == k.Name
select new { master = m, kongfu = k };
联合查询的扩展方法
var res = targetListT.SelectMany(t => targetListK, (t, k) => new { targetT = t, targetK = k }).Where(x => x.targetT.Id = x.targetK.Id);
4.对查询结果做排序
//对查询结果做排序 orderby descending表示倒序
var res = from m in TargetList
where 条件
orderby 排序依据1, 排序依据2 //以依据1为基准 在依据1相同的情况下按依据2排序
select m;
foreach (var item in res)
{
Console.WriteLine(item.ToString());
}
5 join on 集合查询
var res = from t in targetListT
join k in targetListK on t.Id equals k.Id(条件)
where (t.age > 10(条件))
select new { targetT = t, targetK = k };