来自List< MyType>的无效类型转换到IEnumerable< MyType>回到List< MyType&gt ;,为什么呢? [英] Invalid cast from List<MyType> to IEnumerable<MyType> back to List<MyType>, why?

查看:59
本文介绍了来自List< MyType>的无效类型转换到IEnumerable< MyType>回到List< MyType&gt ;,为什么呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以基本上我有这种方法.

So basically i have this method.

public List<Customer> FilterCustomersByStatus(List<Customer> source, string status)
{
    return (List<Customer>)source.Where(c => c.Status == status);
}

我向我抛出了一个无法投射的错误:

I throws me an error that it cannot cast:

无法将类型为"WhereListIterator`1 [AppDataAcces.Customer]"的对象强制转换为类型为"System.Collections.Generic.List`1 [AppDataAcces.Customer]".

Unable to cast object of type 'WhereListIterator`1[AppDataAcces.Customer]' to type 'System.Collections.Generic.List`1[AppDataAcces.Customer]'.

为什么...?因为基础类型是相同的,所以Enumerable.Where在哪里创建WhereListIterator的新实例,如果是的话,为什么有人会这样做,因为那是不必要的性能和功能损失,因为我总是必须创建一个新列表(.ToList( ))

Why...? since the underlying type is the same, does the Enumerable.Where create a new instance of WhereListIterator and if so why would anyone do this, because thats an unnecessary loss of performance and functionality since i always have to create a new list (.ToList())

推荐答案

执行Enumerable.在其中创建WhereListIterator

是的

如果是的话,为什么有人会这么做

and if so why would anyone do this

因为它允许延迟流传输行为.如果Where的使用者仅需要第一个或第二个条目,则不必筛选所有列表.这对于LINQ是正常的.

Because it allows lazy streaming behavior. Where won't have to filter all the list if its consumer wants only first or second entry. This is normal for LINQ.

因为那是不必要的性能和功能损失,因为我总是必须创建一个新列表(.ToList())

because thats an unnecessary loss of performance and functionality since i always have to create a new list (.ToList())

性能和功能损失"来自您的设计.过滤后不需要List<Customer>,因为对其进行任何修改都是没有意义的.

That "loss of performance and functionality" comes from your design. You don't need List<Customer> after filtering, because it's pointless to do any modifications on it.

更新:为什么要实施" 因为它是在IEnumerable而不是IList上实现的.因此,它看起来像IEnumerable,听起来很像IEnumerable.

Update: "why is it implemented so" Because it it implemented over IEnumerable, not IList. And thus it looks like IEnumerable, it quacks like IEnumerable.

此外,以这种方式实现它要容易得多.想象一下,您必须在IList上写Where.其中必须返回IList.应该怎么办?返回原始列表的代理?您每次访问都会遭受巨大的性能损失.返回带有过滤项目的新列表?与Where().ToList()相同.返回原始列表,但删除了所有不匹配的项目?这就是RemoveAll的用途,为什么要使用另一种方法.

Besides, it's just so much easier to implement it this way. Imagine for a moment that you have to write Where over IList. Which has to return IList. What should it do? Return a proxy over original list? You'll suffer huge performance penalties on every access. Return new list with filtered items? It'll be the same as doing Where().ToList(). Return original list but with all non-matching items deleted? That's what RemoveAll is for, why make another method.

记住,LINQ尝试发挥功能,并尝试将对象视为不可变对象.

And remember, LINQ tries to play functional, and tries to treat objects as immutables.

这篇关于来自List&lt; MyType&gt;的无效类型转换到IEnumerable&lt; MyType&gt;回到List&lt; MyType&gt ;,为什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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