我有一个ObservableCollection,它包含另一个ObservableCollection。
ObservableCollection<MyModel> models = new ObservableCollection<MyModel>();
我的模型看起来像这样:
public class MyModel
{
public ObservableCollection<MyModel2> list2 { get; set; }
public string Property1 { get; set; }
}
public class MyModel2
{
public string Property2 { get; set; }
public string Property3 { get; set; }
}
现在我想找到模型中的所有MyModel2项目,其中“Property2”==“test1”和“Property3”==“test2”
我知道如何在一个list2中搜索以找到合适的项目,但我想搜索模型集合中的所有“list2”。
var result = from mod
in list2
where mod.Property2 == "test1" && mod.Property3 == "test2"
select mod;
任何帮助都会感激不尽。
答案 0 :(得分:4)
听起来你想要这样的东西:
var query = from model in models
from model2 in model.list2
where model2.Property2 == "test1" && model2.Property == "test2"
select model2;
或者以非查询表达式形式:
var query = models.SelectMany(model => model.list2)
.Where(model2 => model2.Property2 == "test1"
&& model2.Property == "test2");
答案 1 :(得分:1)
var result =
models.SelectMany(item => item.list2.Where(model => model.Property2 == "test1" && model.Property3 == "test2"));
答案 2 :(得分:1)
Enumerable.SelectMany
和Enumerable.Where
:
models.SelectMany(m =>
m.list2.Where(m2 => m2.Property2 == "test1" && m2.Property3 == "test2"));