可查询 据我了解,在 LINQ to SQL & Object 中,在执行查询之前,我可以添加更多表达式。
在此示例中,当我尝试执行 Query2 时,query1 和 Query2 成为最终查询。我对吗? 或者,如果“Numbers”是一个 DB,我是对的?
int[] Numbers= new int[] { 1, 2, 3, 4 };
IQueryable<int> Query1 = from X in Numbers.AsQueryable()
where X > 1
select X;
IQueryable<int> Query2 = from X in Query1
where X > 2
select X;
//if I do this: Query2.Count() -> before it executes this, those two queries become one query (FinalQuery)?
IQueryable<int> FinalQuery = from X in Query2
where X > 2
select X;
但是,如果我使用 IEnumerable,它会执行 Query1 并获得 IEnumerable 吗?然后它会执行Query2并得到最终的IEnumerable? ;它会执行 2 个查询吗?
最佳答案
If I would use IEnumerable, Would it excute Query1 and get a IEnumerable. Then It would excute Query2 and get the final IEnumerable?
是的。但是他们会串联执行,每个编号X
将“流过”两个查询。这称为“惰性评估”或“延迟执行”。
Would it excute 2 querys ?
是的。时间重叠。
[Using IQueryable] befor it excute this, those two querys become one query (FinalQuery)?
没有。
完全相同的事情会发生在 IQueryable<>
上与 IEnumerable<>
一样.不涉及将优化 where X > 1
的“智能”组件。离开。
关于c# - 我需要确认我对 IQueryable 和 IEnumerable 的理解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12800257/