linq 测试 from bili

            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
          RealName = b.RealName, }; 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();

[MapFrom(typeof(T_Csr_UserAuthor))]
[MapTo(typeof(T_Csr_UserAuthor))]
public class Csr_UserDTO : T_Csr_UserAuthor
{


/// <summary>
/// 用户名
/// </summary>
public String UserName { get; set; }
/// <summary>
/// 姓名
/// </summary>
public String RealName { get; set; }


}

*/

无分页 querysql(SQL语句).ToList<自定义实体>();
有分页 querysql(SQL语句).ToList<自定义实体>().GetPagination(pagination);

 

                var d = querysql(sql).ToList<T_Csr_Info_Extend>();
 or       List<Csr_UserDTO> d = querysql(sql).ToList<Csr_UserDTO>().GetPagination(pagination).ToList();

                if (condition == "Name")
                {
                    d = d.Where(x => x.Name != null && x.Name.Contains(keyword)).ToList();
                }


                if (!startTime.IsNullOrEmpty() && !endTime.IsNullOrEmpty())
                {
                    d = d.Where(w => w.CreateTime >= startTime && w.CreateTime <= Convert.ToDateTime(endTime).AddDays(1).AddSeconds(-1)).ToList();
                }
                return d.GetPagination(pagination).ToList();

 

posted @ 2021-03-13 11:09  enych  阅读(78)  评论(0编辑  收藏  举报