[英]Return maximum list of list in Entity Framework C#
我有这两节课:
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; }
}
我有这个查询:
List<List<Candidate>> lstCandidates =
deserializedProduct.results.Select(i=>i.candidates).ToList();
如您所见,我有一个list<Candidate>
的list<Candidate>
。 每一位候选人都有plate
和confidence
。 我需要对lstCandidates
有最大信心的车牌号。 如何获得这个价值?
使用SelectMany
展平内部列表,然后按confidence
值对项目进行排序并检索第一个值。
var result = deserializedProduct.results.SelectMany(item => item.candidates) //Flatten
.OrderByDescending(item => item.confidence) //Order
.FirstOrDefault()?.plate; // Safely take property of first
?.
是C#6.0 Null传播功能
您可以使用SelectMany
然后使用OrderBy
并使用First
方法。
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
其实很简单
deserializedProduct.results
.SelectMany(k => k.candidates)
.OrderByDescending(k => k.confidence)
.Select(k=>k.plate)
.FirstOrDefault();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.