如何通过[Where]过滤后返回[List<>]的驱动集合?

发布于 2024-11-03 07:41:15 字数 344 浏览 1 评论 0 原文

在以下集合中:

public class PersonsCollection : List<Person>

通过 Where 扩展过滤后如何返回 PersonsCollection 的实例?

personCollections.Where(p => p.Name == "Jack").ToList(); 
// will return IEnumerable<Person>, How to return PersonsCollection?

任何想法!

In the following collection:

public class PersonsCollection : List<Person>

How can I return an instance of PersonsCollection after filtering by Where extension?

personCollections.Where(p => p.Name == "Jack").ToList(); 
// will return IEnumerable<Person>, How to return PersonsCollection?

Any idea!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

世态炎凉 2024-11-10 07:41:15

您必须创建一个新的 PersonsCollection 实例,例如

return new PersonsCollection(personsCollection.Where(p => p.Name == "Jack"))

假设您有一个合适的构造函数。

一般来说,无论如何扩展内置集合(而不是使用组合)都是一个坏主意 - 您确定首先想要这种设计吗?

You´d have to create a new instance of PersonsCollection instead, e.g.

return new PersonsCollection(personsCollection.Where(p => p.Name == "Jack"))

assuming you have an appropriate constructor.

Generally it´s a bad idea to extend the built-in collections anyway (rather than using composition) - are you sure you want this design in the first place?

女皇必胜 2024-11-10 07:41:15

您可以为 IEnumerable 创建扩展方法 ToList()...

public static class PersonCollectionExtensions
{
    public static List<Person> ToList(this IEnumerable<Person> self)
    {
         return new PersonCollection(self);
    }
}

You could to create an Extension Method ToList() for an IEnumerable<Person>...

public static class PersonCollectionExtensions
{
    public static List<Person> ToList(this IEnumerable<Person> self)
    {
         return new PersonCollection(self);
    }
}
原来是傀儡 2024-11-10 07:41:15

像这样的事情:

var pc = new PersonCollection();
pc.AddRange(personCollections.Where(p => p.Name == "Jack").ToArray());
return pc;

Something like this:

var pc = new PersonCollection();
pc.AddRange(personCollections.Where(p => p.Name == "Jack").ToArray());
return pc;
宛菡 2024-11-10 07:41:15

几种可能性:

  1. 您的 PersonsCollection 有一个接受 IEnumerable 的构造函数,因此您可以根据查询结果创建一个新的 PersonsCollection。

  2. 您可以创建一个新的扩展方法 ToPersonsCollection() 来构造 PersonsCollection(可以与上面的组合。)

  3. 您可以更改依赖项,以便其他代码不需要 PersonsCollection,而是可以使用 IEnumerable (这是我推荐的选项。)< /p>

A few possibilities:

  1. Your PersonsCollection has a constructor that accepts an IEnumerable<Person>, so you create a new PersonsCollection based on the query result.

  2. You can create a new extension method ToPersonsCollection() that constructs the PersonsCollection (may be combined with the above.)

  3. You can change your dependencies so that other code doesn't require a PersonsCollection and can instead work with IEnumerable<Person> (this is the option that I'd recommend.)

十雾 2024-11-10 07:41:15

您需要在集合中创建正确的构造函数:

public class PersonsCollection : List<Person> {
  public PersonsCollection() { }
  public PersonsCollection(IEnumerable<Person> persons) : base(persons) { }
}

然后您可以像这样过滤它:

new PersonsCollection(personCollections.Where(p => p.Name == "Jack"));

从您对 CollectionBase 的评论中,我猜您正在重构不使用泛型的旧代码。 Krzysztof Cwalina 发表了一篇关于 如何从非通用集合转换为通用集合。您还可以在博客文章中找到他的评论为什么List 不推荐在公共 API 中使用 有趣的是:

我们建议对输出、属性和接口使用 CollectionReadOnlyCollectionKeyedCollection用于输入的 IEnumerableICollectionIList

您可以简单地摆脱 PersonsCollection 并使用 Collection 之类的东西来代替。

You need to create the proper constructor in your collection:

public class PersonsCollection : List<Person> {
  public PersonsCollection() { }
  public PersonsCollection(IEnumerable<Person> persons) : base(persons) { }
}

You can then filter it like this:

new PersonsCollection(personCollections.Where(p => p.Name == "Jack"));

From your comment about CollectionBase I guess that you are refactoring old code not using generics. Krzysztof Cwalina made a blog post about how to transition from non-generic collections to generic collections. You may also find his comment on a blog post why List<T> isn't recommended in public API's interesting:

We recommend using Collection<T>, ReadOnlyCollection<T>, or KeyedCollection<TKey,TItem> for outputs and properties and interfaces IEnumerable<T>, ICollection<T>, IList<T> for inputs.

You could simply get rid of PersonsCollection and use something like Collection<Person> instead.

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