在LINQ中过滤

时间:2009-10-19 13:44:14

标签: c# linq filtering

从给定的设置

IEnumerable<int> one = new int[] { 1, 2, 3, 4, 5, 6, 7 };

IEnumerable<int> two = new int[] { 12, 34, 56, 7, 8 };


MySet[] sets
= new MySet[]
{ 
   new MySet{ MySetID =100, MySubSet=new MySubSet{SubSet=new List<int>(one), 
   SubSetID=1212}},

   new MySet{ MySetID =101, MySubSet=new MySubSet{SubSet=new List<int>(two),
   SubSetID=1414}}
};

如何从“SubSet”中过滤掉偶数

 var GetSet = 
     from mysets in sets 
    where (P => mysets.MySubSet.SubSet.FindAll(???? ))
    select mysets;

1 个答案:

答案 0 :(得分:1)

这是你想要的吗?你的问题相当令人困惑,但我相信你正在寻找这样的东西:

var query = from mySet in sets
            select new MySet {
                MySetID = mySet.ID,
                MySubSet = new MySubSet {
                    SubSet = mySet.MySubSet.SubSet.Where(p => p % 2 == 0).ToList(),
                    SubSetID = mySet.MySubSet.SubSetID
                }
            };

因此,我将您的问题解释为您希望在MySubSet.SubSet的每个实例中过滤掉MySet中的偶数。