0

i want to filter my report and only show that row which has maximum time first i use

enddate = fr.First().EndDate

but it given me first value occured not the maximum time i try to use

endDate = fr.Max().EndDate

but failed.

jarlh
  • 35,821
  • 8
  • 33
  • 49

4 Answers4

2

try fr.OrderByDescending(x=> x.EndDate).First().EndDate

Martin S.
  • 111
  • 6
0

If you only want to get the max date, you can use LINQ Max method. This is similar to what you tried - but you need to pass in the property that you want to compare to get its Max value.

fr.Max(x => x.EndDate);

Using Max instead of OrderByDescending has performance benefits since you only grab the max value from the list and avoid sorting the whole list.

If you want to get the whole object with the maximum EndDate, check this answer: How to perform .Max() on a property of all objects in a collection and return the object with maximum value - it has multiple approaches to solving your problem.

shtrule
  • 552
  • 7
  • 20
0

If you want data row with latest date, then:

var reqData = (from o in fr
               orderby o.EndDate descending
               select o).FirstOrDefault();

If you want latest date, then:

var latestDate = fr.Max(d => d.EndDate);
Meer
  • 91
  • 5
0

I want to filter my report and only show that row which has maximum time.

So your report is a sequence of Rows, where every Row has a property EndTime. And you want the one and only Row that has the largest value for EndTime.

class Row
{
    public DateTime EndTime {get; set;}
    ... // other properties
}

IEnumerable<Row> report = ...

Are you sure there is only one Row with the largest EndTime? There are no double EndTimes?

If you are sure, or if you don't care which Row you get, as long as it has the largest value for EndTime, you can sort it in descending EndTime order, then select the properties you want, and finally take the FirstOrDefault:

var rowWithLargestEndTime = report.OrderByDescending(row => row.EndTime)

    // Select the row properties you want in your end result:
    .Select(row => new
    {
        ...
    })
    .FirstOrDefault();

If you want the complete Row in your end result, you don't have to do the Select. If you only want the end time:

.Select(row => row.EndTime).FirstOrDefault();

This solution is the best, if a database management system has to do this for you. DBMS are optimized for sorting. However, if your local process has to do this, then it would be a waste to sort all elements if you will be using only the first one. In that case, consider using one of the overloads of Enumerable.Aggregate

The simplest overload, defines the first row as the one with the largest EndTime. Compare the row with the largest EndTime with every other Row, and keep the one which has the largest EndTime.

If your Report has no Rows at all, you have to decide what you want as result.

Row rowWithLargestEndTime = report.Aggregate(
    // parameter accumulator function: 
    // compare the largest found EndTime with the next EndTime
    // keep the one with the largest EndTime
    (rowWithLargestEndTime, nextRow) => (rowWithLargesEndTime.EndTime < nextRow.EndTime) ?
     nextRow :
     rowWithLargestEndTime);

The nice thing is that Aggregate will have to enumerate your sequence only once. OrderBy will enumerate many times.

Harald Coppoolse
  • 24,051
  • 6
  • 48
  • 92