如何转换 C# 的 linq WHERE 语句?

发布于 2024-10-26 18:33:41 字数 383 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

白况 2024-11-02 18:33:41

你最好这样做:

literals.OfType<Tag>().ToList();

这会给你一个 List

您还可以这样做:

var asList = new List<Tag>(literals.OfType<Tag>());

强制转换根本不起作用,因为 LINQ 根据 IEnumerableIQueryable 工作,它们都不使用 List 作为结果。我发布的第二种方法使用 List 的构造函数重载,将 IEnumerable 作为其初始对象集合。同样在这种情况下,LINQ 中的 OfType 方法是一种更简洁、更简短的形式,本质上是使用 Where(x -> x is T) 过滤列表。

此外,在这种情况下,OfType 是一个更好的主意,因为结果是目标类型的 IEnumerableWhere(x => x is T) 将返回原始源类型的 IEnumerable。这就是为什么 (List)literals.Where(x => x is Tag).ToList() 会因无效转换而发出错误。

有关ToList的详细信息,

请参阅OfType

You would be better off doing:

literals.OfType<Tag>().ToList();

This gives you a List<Tag>.

You can also do:

var asList = new List<Tag>(literals.OfType<Tag>());

Casting simply does not work because LINQ works in terms of either IEnumerable<T> or IQueryable<T> which neither use List as a backing implementation for the results. The second method I posted uses a constructor overload of List<T> the takes in an IEnumerable<T> as its initial collection of objects. Also in this scenario the OfType<T> method from LINQ is a much cleaner, shorter form of essentially filtering a list with Where(x -> x is T).

Also, OfType<T> in this scenario is a much better idea, because the result is an IEnumerable<T> of your target type. Where(x => x is T) will return an IEnumerable<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

情话难免假 2024-11-02 18:33:41
literals.Select(x => x as Tag).Where(x => x != null).ToList()

请注意,这将返回新列表。您将无法通过此操作在何处修改原始列表。也可以这样完成: literals.OfType().ToList() 它将返回 IList

更新:修改后的类型

literals.Select(x => x as Tag).Where(x => x != null).ToList()

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 return IList<Tag>

Update: modified type

一枫情书 2024-11-02 18:33:41
List<Tag>)literals.Where(x => x is Tag).ToList();

甚至更好:

literals.OfType<Tag>();

那么你可以从中创建一个新列表:

new List<Tag>(literals.OfType<Tag>());
List<Tag>)literals.Where(x => x is Tag).ToList();

or even better :

literals.OfType<Tag>();

then you can create a new list from it:

new List<Tag>(literals.OfType<Tag>());
倾城月光淡如水﹏ 2024-11-02 18:33:41

根据您想要的效果,您可以这样做:

public List<Tag> Tags
{
    get { return literals.Where(x => x is Tag).ToList(); }
}

请注意,这不是演员表,而是列表的创建!

Depending on the effect you want you could do this:

public List<Tag> Tags
{
    get { return literals.Where(x => x is Tag).ToList(); }
}

Do realize that this is not a cast but the creation of a list!

陌生 2024-11-02 18:33:41

添加 ToList() 怎么样?

你的 getter 变成: literals.Where(x => x is Tag).ToList();

How about adding ToList()?

Your getter becomes: literals.Where(x => x is Tag).ToList();

醉态萌生 2024-11-02 18:33:41

尝试以下操作:

literals.Where(x => x is Tag).Cast<Tag>().ToList();

如果 Tag 派生自 Literal 则有效

try the following:

literals.Where(x => x is Tag).Cast<Tag>().ToList();

it works if Tag is derived from Literal

花开柳相依 2024-11-02 18:33:41

Where 方法返回与源相同的类型,因此您必须转换每个项目,然后使用 ToList 方法从结果创建列表:

return literals.Where(x => x is Tag).Select(x => (Tag)x).ToList();

您还可以我们使用 OfType 方法:

return literals.OfType<Tag>().ToList();

The Where method returns the same type as the source, so you would have to cast each item, then use the ToList method to create a list from the result:

return literals.Where(x => x is Tag).Select(x => (Tag)x).ToList();

You can also us the OfType method:

return literals.OfType<Tag>().ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文