使用Lambda从List接收根级别和嵌套项目

时间:2015-10-21 14:58:26

标签: c# lambda

我有一个这样的课程:

public class Item
{
    private string name;
    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    private bool isAction1Possible;
    public bool IsAction1Possible
    {
        get { return isAction1Possible; }
        set { isAction1Possible = value; }
    }

    private bool isAction2Possible;
    public bool IsAction2Possible
    {
        get { return isAction2Possible; }
        set { isAction2Possible = value; }
    }

    private bool isAction1Selected;
    public bool IsAction1Selected
    {
        get { return isAction1Selected; }
        set 
        { 
            if(IsAction2Selected)
                IsAction2Selected = false;
            isAction1Selected = value; 
        }
    }

    private bool isAction2Selected;
    public bool IsAction2Selected
    {
        get { return isAction2Selected; }
        set 
        { 
            if(IsAction1Selected)
                IsAction1Selected = false;
            isAction2Selected = value; 
        }
    }

    private Dictionary<int, Item> dependencies = new Dictionary<int, Item>();
    public Dictionary<int, Item> Dependencies
    {
        get { return dependencies; }
        set { dependencies = value; }
    }
}

Dependencies Dictionary包含Items类的实例。 我现在有一个List,其中我添加了ItemsIsAction1PossibleIsAction2Possible的所有true,其中正在呈现给用户。用户现在选择他想要进一步处理的项目,这会将IsActionXSelected属性设置为true

最后,我想使用Lambda查询来接收List IsAction1PossibletrueIsAction1Selectedtrue的所有“基础”项目} {strong}和{/ strong> Items Dependencies Dictionary IsAction1Possible中的所有true

我能够使用Select或使用SelectMany的所有依赖关系项接收所有基本项。

这是我尝试的但我最终只得到了基本项而不是依赖项。甚至可以在一个查询中执行它我将不得不为Base项目做一个而对依赖项做一个?谢谢你的任何提示!

IEnumerable<Item> toBeProcessed = ItemsAction1Enabled
                .Where(i => i.IsAction1Possible == true && i.IsAction1Selected == true).ToList()
                .SelectMany(d => d.Dependencies.Values)
                .Where(d => d.Action1Possible == true);

0 个答案:

没有答案