简体   繁体   English

Linq在查询中使用TakeWhile

[英]Linq using TakeWhile in a query

Hi i just started learning linq and have been trying to understand the TakeWhile usage 嗨,我刚刚开始学习linq,并且一直在尝试了解TakeWhile的用法

here i have tried something like this 在这里我尝试过这样的事情

 using (  var db = new OrchestrateDataEntities())
        {


            var y = db.Set<Rulebook_Mapping>();
            var xyz = y.TakeWhile(x => x.ID == 2).AsQueryable();
            foreach (var item in xyz)
            {
                Console.WriteLine(item.ID);


            }

        }
        Console.ReadKey();

and then comes the error 然后出现错误

 LINQ to Entities does not recognize the method 'System.Linq.IQueryable`1[ConFW.Rulebook_Mapping] TakeWhile[Rulebook_Mapping](System.Linq.IQueryable`1[ConFW.Rulebook_Mapping], System.Linq.Expressions.Expression`1[System.Func`2[ConFW.Rulebook_Mapping,System.Boolean]])' method, and this method cannot be translated into a store expression.

Check supported and unsupported methods in LINQ to Entity at http://blogs.msdn.com/b/wriju/archive/2009/01/02/linq-to-entity-skip-and-take-method-does-not-work.aspx . http://blogs.msdn.com/b/wriju/archive/2009/01/02/linq-to-entity-skip-and-take-method-does-not-中检查LINQ to Entity中受支持和不受支持的方法。 work.aspx Then open paging method page, you will see that TakeWhile is not supported (at the bottom). 然后打开分页方法页面,您将看到不支持TakeWhile(在底部)。

qxg is right , TakeWhile is not implemented by EF, but you can use it in LINQ to Object. qxg是正确的 ,TakeWhile不是由EF实现的,但是您可以在LINQ to Object中使用它。

Old code: 旧代码:

var y = db.Set<Rulebook_Mapping>();

New code: 新代码:

var y = db.Set<Rulebook_Mapping>().ToList();

New code should fix the error, it brings all records in RuleBook_Mapping table to local memory, if possible add where filter before ToList() will help the performance. 新代码应该修复该错误,它将RuleBook_Mapping表中的所有记录都带到本地内存,如果可能的话,在ToList()之前添加过滤器将有助于提高性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM