Does anybody know how to access a specific field from List<>? I cant figure out howto access a specific field in newList object.
List<Liner> LX = new List<Liner>();
public class Liner
{
public double Temperature { get; set; }
public double Moisture { get; set; }
}
newList = LX.OrderBy(x => x.Temperature).ToList();
var lstMXLast = newList.GetRange(8755, 5); // I need only 5 specific Moisture records in this case.
GetRangereturns a copy of the list with the given range. So your list needs at least 8760 items. To select only theMoistureproperty of your objects, you can use LINQ’sSelect:Note: you need the
ToListat the end only if you want to persist the query. YourToListat the end of theOrderByquery is useless because you want to chain another query. I would materialze LINQ queries only as late as possible.You could also use LINQ for the whole thing:
Assuming that you originally wanted to select the 5 liners with the highest temperature, this should give you the correct result: