选择包含其他列表中所有元素的列表

时间:2015-09-07 21:54:14

标签: c#

好的,我有两个要素:

class Full
{
    public string FullId {get; set;}
    public List<string> Tapes {get; set;}
}

class OptSet
{
    public string SetId {get; set;}
    public list<string> Tapes2 {get; set;}
}

我需要选择一个Full Tapes列表,其中包含OptSet Tapes2列表中的所有元素。

我需要在Linq中完成。我试过了

Full currFull = inputFull.Where(f => 
    f.Tapes.Contains(OptSet.Tapes2.ToString())).FirstOrDefault();

但这会导致null。

1 个答案:

答案 0 :(得分:0)

这有用吗?

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Full full = new Full()
            {
                FullId = "A",
                Tapes = new List<string>() { "1", "2", "4" }
            };



            List<OptSet> optSet = new List<OptSet>() {
                new OptSet() { SetId = "x", Tapes2 = new List<string>() {"1", "2", "3"}},
                new OptSet() { SetId = "y", Tapes2 = new List<string>() {"1", "2", "4"}},
                new OptSet() { SetId = "z", Tapes2 = new List<string>() {"1", "2", "3", "4"}}
            };


            List<OptSet> results = optSet.Where(x => full.Tapes.Where(y => !x.Tapes2.Contains(y)).Count() == 0).ToList();

        }
    }
    public class Full
    {
        public string FullId { get; set; }
        public List<string> Tapes { get; set; }
    }


    public class OptSet
    {
        public string SetId { get; set; }
        public List<string> Tapes2 { get; set; }
    }

}

​