简体   繁体   English

使用LINQ查询内部IList的属性

[英]Using LINQ to query properties of an inner IList

Below method returns an IList of 'SomeClass', which has property called IList. 下面的方法返回一个'SomeClass'的IList,它具有称为IList的属性。

What I am looking for is if 'Status' property of IList is 'true', I wanted to get the values of IList. 我要寻找的是如果IList的“状态”属性为“ true”,我想获取IList的值。 I can easily do this using foreach loop, but I am planning to use LINQ for this. 我可以使用foreach循环轻松地做到这一点,但是我打算为此使用LINQ。

Definitions : 定义

Method: 方法:

IList<SomeClass> SomeMethod()

public class SomeClass
{
    public SomeClass();

    public IList<SomeInnerClass> ActualData { get; set; }
    public bool Status { get; set; }
}

public class SomeInnerClass
{
    public SomeInnerClass();

    public int? ID { get; set; }
    public string Message { get; set; }
    public string Name { get; set; }
}

What I have now: 我现在所拥有的:

var abc = IList<SomeClass> SomeMethod();

foreach (var outer in abc)
{
    foreach (var inner in outer.ActualData)
    {

    }
}

If you wanted to select the SomeInnerClass objects themselves, you could use a Where() call to filter your outer collection and then a Select() or SelectMany() call to retrieve either the ActualData or SomeInnerClass objects respectively : 如果要自己选择SomeInnerClass对象,则可以使用Where()调用过滤外部集合,然后使用Select()SelectMany()调用分别检索ActualDataSomeInnerClass对象:

// Get your list
var list = SomeMethod(); 
// Pull out on the SomeInnerClasses where Status is true
var output = list.Where(l => l.Status)
                 .SelectMany(l => l.ActualData)
                 .ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM