Ehsan Akbar Ehsan Akbar - 29 days ago 10
C# Question

return maximum list of list in entity framework c#

I have these two classes :

public class Result
{
public string plate { get; set; }
public double confidence { get; set; }
public int matches_template { get; set; }
public int plate_index { get; set; }
public string region { get; set; }
public int region_confidence { get; set; }
public long processing_time_ms { get; set; }
public int requested_topn { get; set; }
public List<Coordinate> coordinates { get; set; }
public List<Candidate> candidates { get; set; }
}

public class Candidate
{
public string plate { get; set; }
public double confidence { get; set; }
public int matches_template { get; set; }
}


I have this query :

List<List<Candidate>> lstCandidates =
deserializedProduct.results.Select(i=>i.candidates).ToList();


as you can see i have a list of
list<Candidate>
. every candidates have
plate
and
confidence
. I need the plate number with maximum confidence in my
lstCandidates
.How can get this value ?

Answer

You can use SelectMany then OrderBy and use First methods.

var candidate = deserializedProduct
                    .results
                    .SelectMany(i=>i.candidates) // Flatten the collection
                    .OrderByDescending(p => p.confidence) // Order by descending on confidence property 
                    .First(); // Take the max
Comments