Reputation: 93
I'm very new to Linq
and having a problem with this code:
double[] values;
private List<double[]> valueList = new List<double[]>();
void AddNewValues(double d1, double d2, double d3)
{
values[0] = d1;
values[1] = d1;
values[2] = d1;
valueList.Add(values);
}
void GetAllFirstValues()
{
var test = valueList.Where(s => s == typeof(double[0]));
}
How to get the first item of each array inside the list?
Is that even possible? Is Linq
here a proper way to do that or is there a more clever solution?
Upvotes: 1
Views: 9237
Reputation: 9509
This should be System.Linq
, you don't need to check for type. It will select first or default if array is empty and return it.
IEnumerable<double> d = l.Select(array => array.FirstOrDefault())
or, if you want to filter only non empty arrays:
IEnumerable<double> d = l.Where(array => array.Any()).Select(array => array.First());
Be aware that array also can be null
, so you can even filter that out within the Where
clause.
Upvotes: 10