首页 > 其他 > 详细

linq 测试 from bili

时间:2021-03-13 11:51:10      阅读:23      评论:0      收藏:0      [点我收藏+]
            var animals = new[] {
                new { Name="Dog",Age=3,Type1="Home" },
                new { Name="Cat",Age=1,Type1="Home" },
                new { Name="Wolf",Age=2,Type1="Wild" },
                new { Name="Monkey",Age=5,Type1="Wild" },

           };

            //查询分组

            var result =
                from a in animals
                group a by a.Type1;

            foreach (var item in result)
            {
                var d = item.Key;

                foreach (var x in item)
                {
                    var dd = x;
                }
            }

            //使用select 

            var result0 =
                from a in animals
                group a by a.Type1 into grp
                select grp;

            var result1 =
                from a in animals
                group a by a.Type1 into grp
                orderby grp.Count()
                select grp;

            var result2 =
                 from a in animals
                 group a by a.Type1 into grp
                 orderby grp.Count() descending
                 select grp;

            var result3 =
                 from a in animals
                 where a.Age > 2
                 group a by a.Type1 into grp
                 where grp.Count() > 2
                 orderby grp.Count() descending
                 select grp;

            var result4 =
                 from a in animals
                 where a.Age > 2
                 group a by a.Type1 into grp
                 where grp.Count() > 2
                 orderby grp.Count() descending
                 select grp;

            //返回自定义类
            var result5 =
                 from a in animals
                 where a.Age > 2
                 group a by a.Type1 into grp
                 where grp.Count() > 2
                 orderby grp.Count() descending
                 select new
                 {
                     Type = grp.Key
                 };

            //用方法名来调用分组
            var r1 = animals.GroupBy(a => a.Type1);

            //join语句

            var foods = new[] {
                new {Name ="Dog",Food="Bone,Meat" },
                new {Name ="Cat",Food="Fish" },
                new {Name ="Wolf",Food="Meat" },
                new {Name ="Monkey",Food="Banana,Apple" }
            };

            var animalFood =
                 from a in animals
                 join f in foods
                 on a.Name equals f.Name
                 select $"{a.Name} like {f.Food}";

            var animalFood0 =
                from a in animals
                join f in foods
                on a.Name equals f.Name
                orderby a.Name
                select $"{a.Name} like {f.Food}";

            //三个表 Join 联查
            var sounds = new[] {
                new { Name="Dog",Sound="汪汪"},
                new { Name="Cat",Sound="喵喵"},
                new { Name="Wolf",Sound="喔喔"},
                new { Name="Monkey",Sound="hi hi"},
            };

            var animalFood1 =
                from a in animals
                join f in foods
                on a.Name equals f.Name
                join s in sounds
                on a.Name equals s.Name
                orderby a.Name
                select $"{a.Name} like {f.Food} . {s.Sound}";

            /*
             * 其他join 查询
            Expression<Func< T_Csr_UserAuthor, T_Sys_User, Csr_UserDTO>> select = (a, b) => new Csr_UserDTO
            {
                SysUserId = b.Id
            };

            select = select.BuildExtendSelectExpre();

            var base_d = GetIQueryable();
            var q = from a in base_d.AsExpandable()
                    join b in Service.GetIQueryable<T_Sys_User>()
                    on a.SysUserId equals b.Id into ab
                    from b in ab.DefaultIfEmpty()
                    select @select.Invoke(a, b);
            var where = LinqHelper.True<Csr_UserDTO>();
            //if (!condition.IsNullOrEmpty() && !keyword.IsNullOrEmpty())
            //{
            //    var newWhere = DynamicExpressionParser.ParseLambda<Csr_ProDTO, bool>(
            //        ParsingConfig.Default, false, $@"{condition}.Contains(@0)", keyword);
            //    where = where.And(newWhere);
            //}
            return await q.Where(where).GetPagination(pagination).ToListAsync();
             
             */

 

linq 测试 from bili

原文:https://www.cnblogs.com/enych/p/14527974.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!