【问题标题】:selecting an individual element from a list within a list using .where and .select使用 .where 和 .select 从列表中的列表中选择单个元素
【发布时间】:2013-02-13 21:35:40
【问题描述】:

我有一个List<> 类型为world 并且在每个世界元素中都有一个List<> 类型为item,它本身包含一个Rectangle 和一个string

这就是世界的结构

`class world
    public Items[] items { get; set; }

    public world(int nooflevels, int noofitems)
    {
        //when creating a world make the items
        items = new Items[noofitems];

        for (int i = 0; i < noofitems; i++)
        {
            items[i] = new Items();
        }
     }        
}`

和项目

class Items
{
    public new Rectangle spriterect;
    public string type { get; set; }

    public Items()
    {
        spriterect.X = 0;
        spriterect.Y = 0;
        spriterect.Width = 0;
        spriterect.Height = 0;
        type = "default";
    }

}

这个世界列表是这样创建的 List&lt;world&gt; Worlds = new List&lt;world&gt;();

我试图根据类型从项目列表中取出一个特定的矩形

    void getitem(string thetype, int world)
    {
        Rectangle  a = Worlds[world].
                       items.Where(f=> f.type == thetype).
                       Select(g => g.spriterect);
    }

所以我希望这会选择包含 .type thetype 的 item[].spriterect 我希望它返回该项目中的矩形,但它返回一个 IEnumerable 我如何让它根据类型返回单个矩形?

【问题讨论】:

    标签: c# string list ienumerable rectangles


    【解决方案1】:

    您应该从项目中选择单个项目。如果应该有指定类型的单个矩形,则使用SingleOrDefault

    var item = Worlds[world].items.SingleOrDefault(i => i.type == thetype);
    if (item != null) 
        a = item.spriterect;
    

    如果您完全确定总是存在指定类型的矩形,那么只需使用Single

    Rectangle a = Worlds[world].items.Single(i => i.type == thetype).spriterect;
    

    【讨论】:

      【解决方案2】:

      您可能想在.Select 之后使用.Single

      如果不完全匹配,Single 会抛出异常。

      Rectangle a = Worlds[world]
                         .items.Where(f=> f.type == thetype)
                         Select(g => g.spriterect).Single();
      

      【讨论】:

        【解决方案3】:

        而不是在哪里使用FirstOrDefault。如果它没有找到该项目,它将返回 null。

        var primary = Worlds[world].FirstOrDefault(f=> f.type == thetype);
        
        if (primary != null)
           return primary.spriterect;
        
        return null;
        

        【讨论】:

          【解决方案4】:

          Where 函数返回一个集合(符合条件的所有内容),而不仅仅是单个项目。您可能想使用FirstSingle,注意如果有多个匹配条件,Single 将抛出异常(如果没有,则两者都将抛出)。

          【讨论】:

            【解决方案5】:

            如果你只知道你只会得到一个值,你可以使用Single,如果你知道该项目可能不存在,则可以使用SingleOrDefault

            //use if you know the rectangle will be there, and there will be only 1 that matches the criteria
            Rectangle a = Worlds[world].items.Single(f => f.type == thetype).spriterect;
            
            //use if the rectangle may not be there, and if it is there will be only 1 that matches the criteria
            var item = Worlds[world].items.SingleOrDefault(f => f.type == thetype);
            if (item != null)
                Rectangle a = item.spriterect;
            

            【讨论】:

            • 您缺少一个选择,因为这会返回一个项目而不是一个矩形。
            猜你喜欢
            • 1970-01-01
            • 2012-08-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-06-08
            • 1970-01-01
            • 2019-11-16
            • 1970-01-01
            相关资源
            最近更新 更多