如何投放名单,LT;对象>列出< SomethingElse> [英] How to cast List<object> to List<SomethingElse>

查看:177
本文介绍了如何投放名单,LT;对象>列出< SomethingElse>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何能投射出列表<对象> 列表< SomethingElse>

,其中 SomethingElse 被称为从下降的对象

(where SomethingElse is known to descend from object)

奖金颤振

铸造名单:

List<Object> first = ...;

List<SomethingElse> second = (List<SomethingElse>)first;



不工作:

doesn't work:

无法将类型'System.Collections.Generic.List'到'System.Collections.Generic.List

Cannot convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List'

铸造名单:

List<SomethingElse> second = first.Cast<SomethingElse>();



不工作:

doesn't work:

无法转换隐含类型'System.Collections.Generic.List'到'System.Collections.Generic.List

Cannot implicitely convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List'

我实际上并不需要完整的列表< T> 的对象,只是一个的ICollection< T> 将尽

i don't actually need the full List<T> object, just an ICollection<T> will do:

ICollection<SomethingElse> second = first;
ICollection<SomethingElse> second = (ICollection<SomethingElse>)first;
ICollection<SomethingElse> second = first.Cast<SomethingElse>();



不起作用。

don't work.

推荐答案

LINQ,通过内部的扩展方法实现的 可枚举 类,依靠延迟执行:

LINQ, as implemented through the extension methods within the Enumerable class, relies on deferred execution:

方法在返回值的序列,直到查询对象列举不消耗目标数据的查询。这被称为延迟执行

Methods that are used in a query that returns a sequence of values do not consume the target data until the query object is enumerated. This is known as deferred execution.

演员LT; T> 不产生一个新的列表立即,而是存储所有需要执行的操作的信息。需要时,该列表将只得到列举的(例如,通过的foreach 语句)。

Cast<T> does not create a new list immediately, but rather stores all the information that is required to perform the action. The list would only get enumerated when required (for example, through a foreach statement).

在你的情况,如果你只是打算遍历序列,你应该考虑坚持到的IEnumerable< T> 接口,这是演员LT的声明的返回类型; T>

In your case, if you simply intend to iterate over the sequence, you should consider sticking to the IEnumerable<T> interface, which is the declared return type of Cast<T>:

IEnumerable<SomethingElse> second = first.Cast<SomethingElse>();
foreach (SomethingElse se in second)
{
    // ...
}

这是有效的,因为它只执行每个项目迭代投。

This is efficient, since it only performs the cast as each item is iterated.

如果你确信你想要一个新的列表要立即创建,使用了ToList

If you’re convinced you want a new list to be created immediately, use ToList:

List<SomethingElse> second = first.Cast<SomethingElse>().ToList();



修改:在回答点张贴在注释:

Edit: Replying to point posted in comment:

这取决于你所说的可修改列表是什么。有几个LINQ查询操作,让你进一步修改您的查询的定义。例如,如果你想删除其所有的 SomethingElse 元素请将isDeleted 属性是真实,您可以使用其中,运营商:

It depends on what you mean by "a list that can be modified". There are several LINQ query operators that will allow you to alter the definition of your query further. For example, if you want to remove all SomethingElse elements whose IsDeleted property is true, you can use the Where operator:

IEnumerable<SomethingElse> second = first.Cast<SomethingElse>();
second = second.Where(element => !element.IsDeleted);

如果要添加新的元素序列,您可以使用 CONCAT 运营商:

If you want to add a sequence of new elements, you can use the Concat operator:

second = second.Concat(anotherCollectionOfSomethingElse);

如果要排序你的编号升序排列顺序,使用排序依据运营商:

If you want to sort your sequence in ascending order of ID, use the OrderBy operator:

second = second.OrderBy(element => element.ID);



每一次,我们将一个查询运营商在我们的查询前定义,分配新(复合)查询我们的第二变量。 LINQ将存储在查询定义所有的运营商。然后,当序列实际上是枚举(例如,通过的foreach 了ToList ),它会给你的您的序列,按顺序应用的所有查询操作的复合结果。

Each time, we’re applying a query operator over the former definition of our query, and assigning the new (composite) query to our second variable. LINQ would store all your operators in the query definition. Then, when the sequence is actually enumerated (for example, through a foreach or ToList), it would give you the composite result of your sequence, with all the query operators applied in order.

与延迟执行/懒惰评估所有情况下,注意不要用这种走极端。如果,例如,你要应用其中,运营商,这将大大减少你的序列的大小,它可能是有意义的热切地执行查询和存储列举列表,而不是

As with all cases of deferred execution / lazy evaluation, be careful not to go overboard with this. If, for example, you’re going to apply a Where operator which will reduce the size of your sequence drastically, it might make sense to execute the query eagerly and store the enumerated list instead.

这篇关于如何投放名单,LT;对象&gt;列出&LT; SomethingElse&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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