2016-11-24 91 views
1

我在下面的类中定义:LINQ过滤器收集在其提交的子集合

public class AssignmentDictionaryDTOChildItem 
{ 
    public int id { get; set; } 
    public string text { get; set; } 
} 

public class AssignmentDictionaryDTO 
{ 
    public string BatchName { get; set; } 
    public List<AssignmentDictionaryDTOChildItem> ChildItems { get; set; } 
} 

,并有一个模型AssignmentDictionaryDTO类型的列表。

现在我想(从AssignmentDictionaryDTOChildItem)

model = model.Where(x => x.ChildItems.SelectMany(y => y.text.ToLower().Contains(q), z => z)); 

文本字段筛选我的模型,但它并不能编译 - 抛出异常下面

The type arguments for method 'System.Linq.Enumerable.SelectMany<TSource,TCollection,TResult>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,System.Collections.Generic.IEnumerable<TCollection>>, System.Func<TSource,TCollection,TResult>)' cannot be inferred from the usage. Try specifying the type arguments explicitly. 

我知道,在上面查询z是AssignmentDictionaryDTOChildItem类型,我的模型是AssignmentDictionaryDTO类型的列表,所以它不适合。 那么如何修复我的查询来实现上面提到的过滤?

回答

1

什么:

model = model.Where(x => x.ChildItems.All(y => y.text.ToLower().Contains(q))).ToList(); 
//or Any instead of All