为什么IOrderedEnumerable在过滤后不保留顺序 [英] Why doesn't IOrderedEnumerable retain order after where filtering

查看:109
本文介绍了为什么IOrderedEnumerable在过滤后不保留顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经简化了这个问题.我有一个有序的IEnumerable,我想知道为什么应用where过滤器会使对象无序

它不会编译,但它应该有潜力

IOrderedEnumerable<int> tmp = new List<int>().OrderBy(x => x);
//Error Cannot Implicitly conver IEnumerable<int> To IOrderedEnumerable<int>
tmp = tmp.Where(x => x > 1);

我知道,如果来自IQueryable(例如,使用linq到某个数据库提供程序)不会有严格的执行顺序.

但是,当与Linq To Object打交道时,会发生什么使您的对象无序的senario,或者为什么没有实现?

编辑

我了解如何正确订购这不是问题.我的问题更多是关于设计的问题.对对象的linq上的Where过滤器应枚举Give枚举并应用过滤.那么为什么我们只能返回IEnumerable而不是IOrderedEnumerable?

编辑

弄清楚何时使用该标签.我正在根据代码中的条件构建查询,我想重用尽可能多的代码.我有一个返回OrderedEnumerable的函数,但是在应用了其他函数后,即使它处于原始有序状态,我也不得不对其重新排序

解决方案

Rene的回答是正确的,但可以使用一些其他解释.

IOrderedEnumerable<T>并不意味着这是一个有序的序列".这意味着此序列已对其应用了排序操作,现在您可以在其后加上ThenBy 以强加其他订购要求."

Where的结果不允许您使用ThenBy跟进它,因此您可能无法在需要IOrderedEnumerable<T>的上下文中使用它.

有道理吗?

但是,当然,正如其他人所说的那样,您几乎总是想先进行过滤,然后再进行排序.这样,您就无需花费时间将要扔掉的物品整理好.

当然,有些时候您必须先订购然后进行过滤;例如,查询女人演唱的前十首歌曲"和查询女人演唱的前十首歌曲"可能有很大的不同!第一个是对歌曲进行排序->排前十名->应用过滤器.第二个是应用过滤器->歌曲排序->进入前十名.

I've created a simplification of the issue. I have an ordered IEnumerable, I'm wondering why applying a where filter could unorder the objects

This does not compile while it should have the potential to

IOrderedEnumerable<int> tmp = new List<int>().OrderBy(x => x);
//Error Cannot Implicitly conver IEnumerable<int> To IOrderedEnumerable<int>
tmp = tmp.Where(x => x > 1);

I understand that there would be no gaurenteed execution order if coming from an IQueryable such as using linq to some DB Provider.

However, when dealing with Linq To Object what senario could occur that would unorder your objects, or why wasn't this implemented?

EDIT

I understand how to properly order this that is not the question. My Question is more of a design question. A Where filter on linq to objects should enumerate the give enumerable and apply filtering. So why is that we can only return an IEnumerable instead of an IOrderedEnumerable?

EDIT

To Clarify the senario in when this would be userful. I'm building Queries based on conditions in my code, I want to reuse as much code as possible. I have a function that is returning an OrderedEnumerable, however after applying the additional where I would have to reorder this even though it would be in its original ordered state

解决方案

Rene's answer is correct, but could use some additional explanation.

IOrderedEnumerable<T> does not mean "this is a sequence that is ordered". It means "this is a sequence that has had an ordering operation applied to it and you may now follow that up with a ThenBy to impose additional ordering requirements."

The result of Where does not allow you to follow it up with ThenBy, and therefore you may not use it in a context where an IOrderedEnumerable<T> is required.

Make sense?

But of course, as others have said, you almost always want to do the filtering first and then the ordering. That way you are not spending time putting items into order that you are just going to throw away.

There are of course times when you do have to order and then filter; for example, the query "songs in the top ten that were sung by a woman" and the query "the top ten songs that were sung by a woman" are potentially very different! The first one is sort the songs -> take the top ten -> apply the filter. The second is apply the filter -> sort the songs -> take the top ten.

这篇关于为什么IOrderedEnumerable在过滤后不保留顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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