从LINQ查询结果中检索ID

时间:2012-10-31 16:01:44

标签: c# linq

我正在使用一个在函数中执行操作(我认为使用LINQ)的库,并返回这样的对象集合:

System.Collections.Generic.IEnumerable<ColorInformation>

这个“ColorInformation”是一个如下所示的类:

public class ColorInformation
{
    private readonly System.Drawing.Color color;
    private readonly string rowTitle;
    private readonly System.Collections.Generic.List<int> ids;

    public ColorInformation(System.Drawing.Color color, string rowTitle,
        System.Collections.Generic.List<int> ids)
    {
        this.color = color;
        this.rowTitle = rowTitle;
        this.ids = ids;
    }

    public string RowTitle
    {
        get { return rowTitle; }
    }

    public System.Drawing.Color Color
    {
        get { return color; }
    }

    public System.Collections.Generic.List<int> Ids
    {
        get { return ids; }
    }
}

我有兴趣检索返回的集合中所有对象的所有ID。我试过这个:

var myids = from ids in theCollectionReturned select ids.Ids;

这给了我一个带有ID的列表集合。我真正想要的只是一个包含所有整数ID的列表。所以,必须在某处进行演员或某种转换,我不知道该怎么做。任何帮助,提示,阅读,代码,示例将不胜感激。 谢谢!

1 个答案:

答案 0 :(得分:8)

  

我真正想要的只是一个包含所有整数ID的列表。

这听起来像你想要的:

var ids = theCollectionReturned.SelectMany(info => info.Ids);