IQueryable和IEnumerable的区别?

  public  static void  IEnumerableAndIQuerable()
        {
            ///我们平时使用的表达式树,是编写的lambda表达式然后编译成的表达式树,
            ///也就是说平时一般情况使用的表达式树都是编译器帮我们完成的
            Expression<Func<Student, bool>> expression = t => t.Name == "农码一生" && t.Sex == "女";
            List<Student> list = new List<Student>() {
                new Student{Name="农码一生",Age=18,Address="长安街",Sex="女"},
                new Student{Name="农码一生",Age=18,Address="长安街",Sex="男"},
                new Student{Name="农码一生",Age=18,Address="长安街",Sex="男"},
                new Student{Name="农码一生",Age=18,Address="长安街",Sex="男"},
                new Student{Name="农码一生",Age=18,Address="长安街",Sex="女"},
            };
            #region  IEnumerable
            //1.第一种情况 
            IEnumerable e1 = list;//里氏替换原则
            foreach (dynamic item in e1)
            {
                item.Address = "3";
                Console.WriteLine(item.Address);

            }
            //2.第二种情况 
            Func<Student, bool> lamb = t => t.Name == "农码一生" && t.Sex == "女";
            // IEnumerable<Student> e3 = list.AsEnumerable<Student>().Where(expression);
            //Where方法不支持写Expressoin 报错
            IEnumerable<Student> e2 = list.AsEnumerable<Student>().Where(expression.Compile());
            //expression.Compile() 转化为Lambda了,IEnuarable中的只支持Lambda写法
            #endregion

            IQueryable<Student> i1 = list.AsQueryable<Student>().Where(expression);
            //Where方法支持expression写法
            Student stu = i1.FirstOrDefault();
        }

参考链接:先说IEnumerable,我们每天用的foreach你真的懂它吗? 

再讲IQueryable<T>,揭开表达式树的神秘面纱

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值