3

我有一个包含另一个 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; }
}

现在我想在“Property2”==“test1”和“Property3”==“test2”的模型中找到所有 MyModel2 项目

我知道如何在一个 list2 中搜索以找到正确的项目,但我想在模型集合中搜索所有“list2”。

var result = from mod 
             in list2
             where mod.Property2 == "test1" && mod.Property3 == "test2"
             select mod;

任何帮助将不胜感激。

4

3 回答 3

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");
于 2012-12-11T12:57:06.503 回答
1
var result =
    models.SelectMany(item => item.list2.Where(model => model.Property2 == "test1" && model.Property3 == "test2"));
于 2012-12-11T12:59:10.207 回答
1

Enumerable.SelectManyEnumerable.Where在“内部”列表中:

models.SelectMany(m => 
    m.list2.Where(m2 => m2.Property2 == "test1" && m2.Property3 == "test2"));
于 2012-12-11T12:59:25.790 回答