如何转换 C# 的 linq WHERE 语句?
我有一个 Literal 类,并且有一个 Tag 继承自它。
我想做以下事情,但我得到了
Unable to cast object of type 'WhereListIterator`1[Core.Literal]' to type 'System.Collections.Generic.List`1[Core.Tag]'.
private List<Literal> literals;
public List<Tag> Tags
{
get { return (List<Tag>)literals.Where(x => x is Tag); }
}
感谢
I have a class Literal and a Tag is inheriting from it.
I would like to do the following but I am getting
Unable to cast object of type 'WhereListIterator`1[Core.Literal]' to type 'System.Collections.Generic.List`1[Core.Tag]'.
private List<Literal> literals;
public List<Tag> Tags
{
get { return (List<Tag>)literals.Where(x => x is Tag); }
}
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
你最好这样做:
这会给你一个
List
。您还可以这样做:
强制转换根本不起作用,因为 LINQ 根据
IEnumerable
或IQueryable
工作,它们都不使用 List 作为结果。我发布的第二种方法使用List
的构造函数重载,将IEnumerable
作为其初始对象集合。同样在这种情况下,LINQ 中的OfType
方法是一种更简洁、更简短的形式,本质上是使用Where(x -> x is T)
过滤列表。此外,在这种情况下,
OfType
是一个更好的主意,因为结果是目标类型的IEnumerable
。Where(x => x is T)
将返回原始源类型的IEnumerable
。这就是为什么(List)literals.Where(x => x is Tag).ToList()
会因无效转换而发出错误。有关ToList的详细信息,
请参阅OfType
You would be better off doing:
This gives you a
List<Tag>
.You can also do:
Casting simply does not work because LINQ works in terms of either
IEnumerable<T>
orIQueryable<T>
which neither use List as a backing implementation for the results. The second method I posted uses a constructor overload ofList<T>
the takes in anIEnumerable<T>
as its initial collection of objects. Also in this scenario theOfType<T>
method from LINQ is a much cleaner, shorter form of essentially filtering a list withWhere(x -> x is T)
.Also,
OfType<T>
in this scenario is a much better idea, because the result is anIEnumerable<T>
of your target type.Where(x => x is T)
will return anIEnumerable<T>
of the original source's type. So that's why(List<Tag>)literals.Where(x => x is Tag).ToList()
emit an error for invalid casts.More information on ToList
More information on OfType
请注意,这将返回新列表。您将无法通过此操作在何处修改原始列表。也可以这样完成:
literals.OfType().ToList()
它将返回IList
更新:修改后的类型
Note that this will return new list. You won't be able to do where and modify original list by this. Also this can be done like this:
literals.OfType<Tag>().ToList()
and it will returnIList<Tag>
Update: modified type
甚至更好:
那么你可以从中创建一个新列表:
or even better :
then you can create a new list from it:
根据您想要的效果,您可以这样做:
请注意,这不是演员表,而是列表的创建!
Depending on the effect you want you could do this:
Do realize that this is not a cast but the creation of a list!
添加 ToList() 怎么样?
你的 getter 变成:
literals.Where(x => x is Tag).ToList();
How about adding ToList()?
Your getter becomes:
literals.Where(x => x is Tag).ToList();
尝试以下操作:
如果
Tag
派生自Literal
则有效try the following:
it works if
Tag
is derived fromLiteral
Where
方法返回与源相同的类型,因此您必须转换每个项目,然后使用ToList
方法从结果创建列表:您还可以我们使用
OfType
方法:The
Where
method returns the same type as the source, so you would have to cast each item, then use theToList
method to create a list from the result:You can also us the
OfType
method: