简体   繁体   English

如何过滤C#中另一个列表的一部分的子列表

[英]How to filter a sub list which is part of another list in C#

I want to filter the list which is already part of another list. 我想过滤已经属于另一个列表的列表。

Consider, 考虑,

Class MainClass 类MainClass

  properties

      String Name

      String MainAddress

      List<SubClass> ExtraInfo

Class SubClass 类子类

  properties

      String Address

      String City

      String PhoneNo

Now i have 10 items in my List and each item in List has 2 ExtraInfo items this is List items. 现在我列表中有10个项目,列表中的每个项目都有2个ExtraInfo项目,这就是列表项目。

Now i want to filter List items with Condition City == "New York". 现在我想用条件城市==“纽约”来过滤列表项目。

So, all 10 items in the main list (List) should have the ExtraInfo (List) items based on the filter condition. 因此,基于过滤条件,主列表(列表)中的所有10个项目都应具有ExtraInfo(列表)项目。

I mean i just want to filter the Sub list not the main list. 我的意思是我只想过滤子列表而不是主列表。

Thanks in Advance! 提前致谢!

Sample Data Name MainAddress ExtraInfo Address City Phone No 样本数据名称MainAddress ExtraInfo地址城市电话号码
1. Vimal Bangalore North Street New York 654564646 --->Sub item 1 North Street California 464654565 --->Sub item 2 1. Vimal班加罗尔纽约北街654564646 --->子项目1加利福尼亚州北街464654565 --->子项目2

  1. Hareesh Chennai North Street Washington 546466466 --->Sub item 1 Tower B New York 464645465 --->Sub item 2 哈雷什·钦奈北街华盛顿546466466 --->子项目1纽约B座464645465 --->子项目2

After Filter (Condition City == "New York"), I should get the output in the main list like 过滤后(条件城市==“纽约”),我应该在主列表中得到输出

 Name      MainAddress       ExtraInfo
                                Address        City        Phone No      

1. Vimal Bangalore North Street New York 654564646 --->Sub item 1 1. Vimal班加罗尔纽约北街654564646 --->子项目1

  1. Hareesh Chennai Tower B New York 464645465 --->Sub item 1 哈雷什·钦奈B座纽约464645465 --->子项目1

Hope this helps! 希望这可以帮助!

I might not have understood the question correctly, but: 我可能未正确理解问题,但是:

List<MainClass> mainList = ...
mainList.ForEach(m => m.ExtraInfo.RemoveAll(subClass => subClass.City != "New York"));

This will modify each item in mainList in place, by removing all items from its list-member ExtraInfo (in place) for which each item in the list (of type SubClass ) does not have "New York" as its City . 通过从列表成员ExtraInfo删除所有项目(就位),列表中的每个项目(类型为SubClass )都没有“ New York”作为其City ,这将修改mainList中的每个项目。 Is that what you want? 那是你要的吗?

If you simply want all of the 'embedded' ExtraInfo items that match the filter no matter which MainClass's list they belong to, you could do this: 如果您只是希望所有与过滤器匹配的“嵌入式” ExtraInfo项目,无论它们属于哪个MainClass列表,都可以这样做:

var filteredSubClasses = mainList.SelectMany(m => m.ExtraInfo)
                                 .Where(subClass => subClass.City == "New York");

EDIT: Based on the OP's comment: This is a bit ugly, but should be what you need: 编辑:基于OP的评论:这有点难看,但是应该是您所需要的:

var filteredList = mainList.Select(m => new MainClass
                            { 
                               Name = m.Name;
                               MainAddress = m.MainAddress;
                               ExtraInfo = m.ExtraInfo
                                            .Where(subClass => subClass.City == "New York")
                                            .ToList()
                            })
                          .ToList();

IMO, it would be easier if MainClass supplied a copy-constructor or clone-method to remove the need to copy all the properties over one by one. IMO,如果MainClass提供了一个复制构造函数或克隆方法会更容易,而不需要一一复制所有属性。 This is a bit brittle - it will break if you add a new property to MainClass . 这有点脆弱-如果将新属性添加到MainClassMainClass

You have to filter each of the sublists: 您必须过滤每个子列表:

List<MainClass> mainList = ...
foreach(MainClass main in mainList)
{
    ICollectionView view = CollectionViewSource.GetDefaultView(main.ExtraInfo);
    view.Filter = ExtraInfoFilter;
}

bool ExtraInfoFilter(object obj)
{
    ExtraInfo item = (ExtraInfo)obj;

    return item.City == "New York";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM