How can I access the string
values in IEnumerable
Results View, where the results may vary?
I am using a foreach
loop to iterate through a list
, and then grabbing the distinct values of VehicleID
:
foreach (var trip in paidTrips) {
var vehicleids = trip
.Select(x => x.VehicleID)
.Distinct()
.ToList();
I know how to do this:
string n = vehicleids[0];
string o = vehicleids[1];
But that gives me an "Index was out of range"-exception if there's more than two items.
I am thinking it would be good to use a for loop to iterate through the indexes and create an array out of them? If so, how could I do that?
try this
IEnumerable<string> vehicleIds=paidTrips
.Select(x => x.VehicleID)
.Distinct()
.ToList();
//or ToArray();
you can acces using foreach loop
foreach( var id in vehicleIds)
{
Console.WriteLine(id);
}
or just using index
var vehicleId0 = vehicleids[0];
//or
var vehicleId1=vehicleId[1];
or using linq
var vehicleId=vehicleIds.FirstOrDefault(x=> x=="1223");