我真的需要在收集使用AsQueryable已()? [英] Do i really need use AsQueryable() on collection?

查看:198
本文介绍了我真的需要在收集使用AsQueryable已()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如code:

List<Student> Students = new List<Student>()   
{   
    new Student(101, "Hugo", "Garcia", new List<int>() { 91, 88, 76, 93 }),  
    new Student(102, "Rick", "Adams", new List<int>() { 70, 73, 66, 90 }),  
    new Student(103, "Michael", "Tucker", new List<int>() { 73, 80, 75, 88 }),  
    new Student(104, "Fadi", "Fakhouri", new List<int>() { 82, 75, 66, 84 }),  
    new Student(105, "Peter", "Barrows", new List<int>() { 67, 78, 70, 82 })  
};

var query = from student in Students
            where student.Marks.AsQueryable().All(m => m > 70)
            select student;

foreach (Student student in query)
{
    Console.WriteLine("{0} {1}<br />", student.FirstName, student.LastName);
}

但是,如果我更改查询到

But if I change the query to

var query = from student in Students
            where student.Marks.All(m => m > 70)
            select student;

这也适用,并产生相同的结果,所以有什么区别?

This also works and produces the same result, so what's the difference?

推荐答案

IQueryable的需要/推荐对象即将从远程数据源(如从数据库)。

IQueryable is required/recommended for objects coming from remote source (like from database).

有关在内存中的集合是没有用的。

For in memory collections it is of no use.

AsQueryable已时使用EX pression树是构造

AsQueryable is used when expression tree is to be constructed.

我能想到的场景的地方是最合适的。在您的例子让说,你需要根据学生的ID从数据库中的一些信息。

I can think of scenario where it is best fit. In your example let say you require some information from database based on student ID.

现在的学生是在内存中的集合。你需要根据学生证火灾的数据库查询。

Now student is in memory collection. You need to fire database query based on student ID.

  var studentList = Students.Select(s => s.Id).AsQueryAble().Select(i => remoteDBProvider.GetInfo(i));

上studentList任何进一步动作进行从IQueryable的接口(查询前pression)调用,将获取只有那些从数据源,这应被返回作为最终的查询结果的记录(只要数据源,返回 remoteDBProvider.GetInfo 的例子值,支持的 QueryProvider )。

Any further operation on the studentList will be invoked from IQueryAble interface ( query expression), and will fetch only those records from data source, which should be returned as final query result (as long as data source, return value of remoteDBProvider.GetInfo in the example, supports QueryProvider).

这篇关于我真的需要在收集使用AsQueryable已()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆