Select Id(s) using Linq

Arun picture Arun · Jan 18, 2014 · Viewed 26.8k times · Source

I have a DynamicNode named products which has product name and product id. I need to select all products id's to a array from the DynamicNode products using LINQ.

I tried something like

 @helper PrintProductYearChart(IEnumerable<DynamicNode> products)
{

    var res = products.select(x => x.filelds['Id']).ToAarray();
}

but its not working correctly.

Can any one help.Thanks in advance

Answer

BRAHIM Kamel picture BRAHIM Kamel · Jan 18, 2014

The Select LINQ operator Projects each element of a sequence into a new form. what you are you doing will project only one element with 'Id' as index so it will return one element only not an array of Id's

here you should specifiy that you want the ID

   @helper PrintProductYearChart(IEnumerable<DynamicNode> products)
    {

        var res = products.select(x => x.Id).ToArray();
    }