简体   繁体   English

Linq如何在IOrderedEnumerable方法之后使用IEnumerable方法?

[英]How does Linq use IEnumerable methods after an IOrderedEnumerable method?

In Linq, extension methods like Where return an IEnumerable collection, but sorting methods like OrderBy return an IOrderedEnumerable collection. 在Linq中,扩展方法如Where返回IEnumerable集合,但排序方法如OrderBy返回IOrderedEnumerable集合。

So, if you have a query that ends with OrderBy (ie returns an IOrderedEnumerable ), you can't later append a Where method - the compiler complains about the type being passed into Where . 所以,如果你有一个以OrderBy结尾的查询(即返回一个IOrderedEnumerable ),你以后就不能附加一个Where方法 - 编译器会抱怨传递给Where的类型。

var query = Process.GetProcesses()
            .Where(p => p.ProcessName.Length < 10)
            .OrderBy(p => p.Id);

query = query.Where(p => p.ProcessName.Length < 5);

However, if you do it all in one query, it's fine! 但是,如果你在一个查询中完成所有操作,那很好!

var query = Process.GetProcesses()
            .Where(p => p.ProcessName.Length < 10)
            .OrderBy(p => p.Id)
            .Where(p => p.ProcessName.Length < 5);

I've looked at the assembly in Reflector to see if the compiler was re-ordering any of the operations, but it doesn't seem to have. 我查看了Reflector中的程序集,看看编译器是否正在重新排序任何操作,但它似乎没有。 How does this work? 这是如何运作的?

IOrderedEnumerable<T> extends IEnumerable<T> so you can still use any of the extension methods. IOrderedEnumerable<T>扩展IEnumerable<T>因此您仍然可以使用任何扩展方法。 The reason your first block of code didn't work is because you had effectively written: 你的第一段代码不起作用的原因是因为你有效地写了:

IOrderedEnumerable<Process> query = Process.GetProcesses()
                                           .Where(p => p.ProcessName.Length < 10)
                                           .OrderBy(p => p.Id);

// Fail: can't assign an IEnumerable<Process> into a variable 
// of type IOrderedEnumerable<Process>
query = query.Where(p => p.ProcessName.Length < 5);

That fails because query.Where(...) only returns an IEnumerable<Process> , which can't be assigned to the query variable. 这失败是因为query.Where(...)仅返回IEnumerable<Process> ,无法将其分配给query变量。 It's not calling Where that's the problem - it's assigning the result back to the original variable. 它没有调用Where的问题 - 它将结果分配回原始变量。 To demonstrate that, this code will work just fine: 为了证明这一点,这段代码可以正常工作:

var query = Process.GetProcesses()
                   .Where(p => p.ProcessName.Length < 10)
                   .OrderBy(p => p.Id);

// Introduce a new variable instead of trying to reuse the previous one
var query2 = query.Where(p => p.ProcessName.Length < 5);

Alternatively, you can declare query to be IEnumerable<T> to start with: 或者,您可以将查询声明为IEnumerable<T>以开头:

IEnumerable<Process> query = Process.GetProcesses()
                                    .Where(p => p.ProcessName.Length < 10)
                                    .OrderBy(p => p.Id);

// Fine
query = query.Where(p => p.ProcessName.Length < 5);

暂无
暂无

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

相关问题 由于Array不强制IEnumerable,Array如何使用LINQ扩展方法 <T> 接口 - How can Array use LINQ extension methods since Array does not implent IEnumerable<T> interface Linq使用IOrderedEnumerable的智能程度和位置 - How intelligent is Linq with IOrderedEnumerable and Where 返回IOrderedEnumerable的方法是否有利? <T> 而不是IEnumerable <T> ? - Can it be advantageous for a method to return IOrderedEnumerable<T> instead of IEnumerable<T>? IOrderedEnumerable的LINQ优化 - LINQ Optimizations for IOrderedEnumerable 您如何在IEnumerable的子列表成员上使用(或可以使用)Linq Intersect方法? - How do you use (or can you use) the Linq Intersect method on a child list member of an IEnumerable? 无法隐式转换类型System.Collections.Generic.Ienumerable <string> 到system.Linq.IOrderedEnumerable <AnonymousType#1> - can not implicitly convert type System.Collections.Generic.Ienumerable<string> to system.Linq.IOrderedEnumerable<AnonymousType#1> 如何将 Group By 与 Task 一起使用<ienumerable<entity> &gt; 在 LINQ </ienumerable<entity> - How to use Group By with Task<IEnumerable<Entity>> in LINQ System.Linq.IOrderedEnumerable不包含“ ToList”的定义 - System.Linq.IOrderedEnumerable does not contain a definition for 'ToList' Linq IEnumerable扩展方法 - 如何提高性能? - Linq IEnumerable Extension Method - How to improve performance? 如何:使用LINQ自定义扩展方法的异步方法 - How to: Use async methods with LINQ custom extension method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM