2016-11-24 94 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