2016-11-05 300 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空傳播特徵