2016-11-05 164 views
1

名单的最大名单上有以下两类:返回实体框架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>列表。每个候选人都有plateconfidence。我需要最大的信心在我的lstCandidates板数。如何获得这个值?

+0

你尝试过什么吗?我会很乐意提供帮助,但是您必须展示您所尝试的内容,以便我们帮助您纠正它。你是否正在寻找max'confidence'值,或者你在寻找具有最大'confidence'值的'Candidate'项目。 –

+0

@GiladGreen你知道我想使用foreach解决方案,传统的解决方案。但还没有编码。我想知道也许有更好的解决方案 –

+0

@GiladGreen我需要最大信心的板数 –

回答

2

可以使用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 
1

其实这很简单

deserializedProduct.results 
    .SelectMany(k => k.candidates) 
    .OrderByDescending(k => k.confidence) 
    .Select(k=>k.plate) 
    .FirstOrDefault(); 
+0

OrderByDescending(k => k.confidence )语法错误 –

+0

@EhsanAkbar你在什么集合中应用这个列表?然后看到我的编辑。 –

2

使用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空传播特征