簡體   English   中英

LINQ:獲取列表中列表的所有項目

[英]LINQ: Get all items of a list within a list

我有一個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來找到合適的項目,但我想搜索models-collection中的所有“list2”。

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

任何幫助將不勝感激。

這聽起來像你想要的東西:

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");
var result =
    models.SelectMany(item => item.list2.Where(model => model.Property2 == "test1" && model.Property3 == "test2"));

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

models.SelectMany(m => 
    m.list2.Where(m2 => m2.Property2 == "test1" && m2.Property3 == "test2"));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM