c# Linq的一些写法

   public class QjcxAll1
   {
        public string id { get; set; }
        public string name { get; set; }
        public int age { get; set; }
        public string sex { get; set; }
        public int socre { get; set; }
   }
    public class Company
   {
        public string companyname { get; set; }
        public List<QjcxAll1> list { get; set; }
        public Company(string _name, List<QjcxAll1> Qjcxlist)
        {
            companyname = _name;
            list = Qjcxlist;
        }
   }
//方法语法
 IEnumerable<T> query=源集合.Where(过滤,必须bool表达式).OrderBy(排序条件).Select(选择条件)
  //查询表达式
 var 结果集=from n in 数据源 where 过滤表达式 order by 排序 select 投影选择

 Lambda表达式广泛用于LINQ中。
 无参 ()=>{处理语句}
 一个参数 参数=>{处理语句}
 多个参数 (参数1,参数2,参数3.....参数N)=>{处理语句}

            //Linq查询IEnumerable集合
            List<QjcxAll1> strlist = new List<QjcxAll1>()
            {
                new QjcxAll1(){id="1",name="Tutorials123",age=12,sex="女",socre=-1 },
                new QjcxAll1(){id="2",name="2222",age=12,sex="男",socre=67 },
            };
            List<QjcxAll1> strlist1 = new List<QjcxAll1>()
           {
               new QjcxAll1(){id="3",name="123",age=12,sex="女",socre=8888 },
                new QjcxAll1(){id="4",name="444",age=12,sex="男",socre=99999 },
           };


           var sums = strlist.Where(x => { return x.socre > 0; }).Sum(a => a.socre);

            var sum1 = (from i in strlist
                        where i.socre > 0
                        select i.socre).Sum();


           var sums1 = strlist.Max(a => a.socre);

            var sum12 = (from i in strlist
                         select i.socre).Min();

           var user = strlist.Where(x => x.socre == 1).FirstOrDefault();//如果strlist中不存在socre=1的分数,用Single和First就会报错

            var jiangxu = strlist.OrderByDescending(x => x.socre).ToList();
            var shengxu = strlist.OrderBy(x => x.socre).ToList();

            //分组ToLookup和GroupBy

          var lookup = strlist.ToLookup(x => x.sex);  //ILookup<TKey, TSource> ToLookup<TSource, TKey>
            foreach (var item in lookup)
           {
                string keys = item.Key;
               foreach (var sub in item)
                {

               }
           }

            var dic = strlist.GroupBy(x => x.sex);//IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>
            foreach (var item in dic)
            {
                string keys = item.Key;
               foreach (var sub in item)
               {

                }
           }

            //内连接
            List<QjcxAll1> Schlist = new List<QjcxAll1>();

            var temp = from i in strlist
                       join sch in Schlist on i.name equals sch.name
                      select new { name = i.name, age = i.age };

            //类型查找
            List<object> objlist = new List<object>() { 1, "2", false, "s", new QjcxAll1() { name = "xx" } };
            IEnumerable<bool> query1 = objlist.OfType<bool>();

           foreach (var item in query1)
            {
                Console.WriteLine(item);
           }

            //查找深层嵌套SelectMany
           Company mobile = new Company("中国移动", strlist);
            Company unicom = new Company("中国联通", strlist1);
            List<Company> companylist = new List<Company>() { mobile, unicom };

            var selectlist = companylist.SelectMany(a => a.list).Where(x => x.sex == "女");
           foreach (var item in selectlist)
            {
                Console.WriteLine(item.name + item.sex);
            }

            Dictionary<string, List<string>> dic1 = new Dictionary<string, List<string>>();
            List<string> list1 = new List<string>() { "1", "2", "3" };
            List<string> list2 = new List<string>() { "13", "14", "15" };
            dic1.Add("1", list1);
            dic1.Add("2", list2); //我想计算value的count。怎么写linq。

          var selectdic = dic1.SelectMany(x => x.Value).Count();
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值