简体   繁体   English

如何在另一个列表中过滤对象列表

[英]How to filter a List of objects inside another list

I need some help to filter a list of objects inside another list of objects. 我需要一些帮助来过滤另一个对象列表中的对象列表。

Currently I am able to filter the FirstLevel object to return only the SecondLevel objects that matches the criteria (having the Id within the list), and if it happens to have a ThirdLevel filter, I need to return only the SecondLevel containing the ThirdLevel object that matches the criteria. 目前,我能够过滤FirstLevel对象以仅返回与条件匹配的SecondLevel对象(列表中具有Id),并且如果它碰巧具有ThirdLevel过滤器,我仅需要返回包含ThirdLevel对象的SecondLevel对象即可符合条件。

What I need is for the ThirdLevel array to contain only the ones that matches the criteria. 我需要的是ThirdLevel数组仅包含符合条件的数组。

Am I able to do that within a single Linq query? 我可以在单个Linq查询中做到这一点吗?

EDIT: Humanly understandable(??) example: Let's say I have a closet, and in this closet I have lots of drawers. 编辑:人类可理解的(??)示例:假设我有一个壁橱,在这个壁橱中我有很多抽屉。 Now in these drawers I have several pairs of socks, each with a diferent color. 现在,在这些抽屉中,我有几双袜子,每双袜子的颜色都不同。 What I need is to be able to filter the drawers to show me only those that contain black socks while removing the other colors from the drawer. 我需要的是能够对抽屉进行过滤,以仅向我显示包含黑色袜子的那些,同时从抽屉中去除其他颜色。 (dunno if that made sense) (不知道是否合理)

Below is an example code: 下面是一个示例代码:

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace MyNamespace
{
    public class Program
    {
        public static void Main(string[] args)
        {
            List<int> filterSecondLevel = new List<int>(){1, 2};
            List<int> filterThirdLevel = new List<int>(){3};

            ThirdLevel _3a = new ThirdLevel(){Id=1};
            ThirdLevel _3b = new ThirdLevel(){Id=2};
            ThirdLevel _3c = new ThirdLevel(){Id=3};
            ThirdLevel _3d = new ThirdLevel(){Id=4};

            List<ThirdLevel> _3la = new List<ThirdLevel>(){_3a,_3b};
            List<ThirdLevel> _3lb = new List<ThirdLevel>(){_3c,_3d};

            SecondLevel _2a = new SecondLevel(){ Id=1, ThirdLevelList=_3la};
            SecondLevel _2b = new SecondLevel(){ Id=2, ThirdLevelList=_3lb};

            List<SecondLevel> _2la = new List<SecondLevel>(){_2a,_2b};

            FirstLevel _1a = new FirstLevel(){ Id=1, SecondLevelList=_2la};

            var result = _1a.SecondLevelList.Where(x => 
                                                          (filterSecondLevel.Count == 0 || filterSecondLevel.Contains(x.Id)) && 
                                                              x.ThirdLevelList.Where(y => 
                                                                  filterThirdLevel.Count == 0 || filterThirdLevel.Contains(y.Id)
                                                      ).ToList().Count > 0
                                                  ).ToList();
        }
    }

    public class FirstLevel
    {
        public int Id {get;set;}
        public List<SecondLevel> SecondLevelList { get; set; }
    }

    public class SecondLevel
    {
        public int Id {get;set;}
        public List<ThirdLevel> ThirdLevelList { get; set; }
    }

    public class ThirdLevel
    {
        public int Id {get;set;}
    }
}

you need to use ANY() 您需要使用ANY()

            var result = _1a.SecondLevelList.Where(x =>
                                                          (filterSecondLevel.Count == 0 || filterSecondLevel.Contains(x.Id)) &&
                                                              x.ThirdLevelList.Where(y => filterThirdLevel.Contains(y.Id)).Any())
                                                              .ToList();

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

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