开发者

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

开发者 https://www.devze.com 2023-02-07 22:45 出处:网络
I want to retrieve all single properties from a collection as an array: class Foo{ public string Bar { get; set; }

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

精彩评论

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