LINQ语句查询

在这里使用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 };

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值