简体   繁体   中英

How can i achieve this in LINQ-Queries?

List<double> Values = new List<double>();
foreach (var item in Level.Items)
{
    Values.Add(item.Keys);
}

I'm new for LINQ-Queries, So can anyone help how to write the above codes in LINQ-Queries.

Try this:

List<double> Values = new List<double>(Level.Items.Select(item => item.Keys));

Or

List<double> Values = Level.Items.Select(item => item.Keys).ToList();

In case you have to add items into existing list:

List<double> Values = new List<double>();

...

Values.AddRange(Level.Items.Select(item => item.Keys));

Use Select :

List<double> values = Level.Items.Select(item => item.Keys).ToList();

If Keys is a collection by its own then use SelectMany :

List<double> values = Level.Items.SelectMany(item => item.Keys).ToList();

你可以这样做

List<double> Values = Level.Items.Select(a => a.Keys).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM