开发者

How can I retrieve all property values from a list of common objects?

I want to retrieve all single properties from a collection as an array:

class Foo{
   public string Bar { get; set; }
   public string Baz { get; set;}
}

I want to get i.e. all Ba开发者_Python百科r properties from the collection

var list = new List<Foo>();

string[] allBars = list. .... 

and how does it go on???

Thanks for any help.


You can use:

string[] allBars = list.Select(foo => foo.Bar).ToArray();

I would only convert this to an array if you specifically need it to be in an array. If your goal is just to output the "Bar" list, you could just do:

var allBars = list.Select(foo => foo.Bar); // Will produce IEnumerable<string>
foreach(var bar in allBars)
{
    // Do something with bar
}


var query = from foo element in foo_bar_list
                        where foo.Bar == ""
                        select new
                        {
                            class1.Baz
                        };
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜