2017-02-20 94 views
0

我有一對多的字典,我需要查找數據中發送給用戶的任何差異。下面是我的查詢C#LINQ查找一對多差異

var FinalQuery = from final in masterquery 
       where final.gpa_vsl != string.Empty || final.eta1_datetime.Date.CompareTo(final.eta_date.Date) != 0 || 
       final.ata_date > DateTime.MinValue || final.cfit_vsl != final.gpa_vsl 
       group final by final.file_no into newGroup 
       orderby newGroup.Key 
       select newGroup; 
var Finaldict = FinalQuery.ToDictionary(g => g.Key, g => g.ToList()); 

foreach(var file in FinalDict) { 
    if (file.Value.Count() > 1) { 
    //Here I need to find out if all eta1 and all vessel match 
    //below is what I am trying to do with Linq 

    bool match = from f in file.Value 
      where file.Value.Count(f => f.eta1_date.CompareTo(f.eta1_date)) = file.Value.Count() 
      select true; 

    //What I am trying to do is if all eta1 dates match return a total int to compare to the total count of values. 
    } 
} 

解釋是這樣的:

Key - FileNo = 123 
Value - Container = 123 , eta1 = 2/20/17, ata1 = 2/20/17, vessel = boat1 
Value - Container = 456, eta1 = 2/18/17, ata1 = 2/18/17, vessel = boat2 
Value - Container = 789, eta1 = 2/20/17, ata1 = 2/20/17, vessel = boat1 
Value - Container = 987 , eta1 = 2/20/17, ata1 = 2/20/17, vessel = boat1 

我願意接受任何和所有的建議。需要找到容器456信息與其他信息不匹配。希望我提供足夠的信息

+0

你可以闡述什麼是你的榜樣字典中的差異? – pquest

+0

我們的目標是讓所有eta1,ata1和船隻匹配。在上面的eta1不符合eta1的其餘部分。如果全部不匹配,那麼我需要向用戶返回一個異常 –

+0

只需注意'orderby'後跟''.ToDictionary'破壞了順序,因爲字典在枚舉時不保證任何順序 – tinudu

回答

1

快速勝利將是:

bool match = file.Value.GroupBy(f => new {f.eta1, f.ata1, f.vessel}).Take(2).Count() == 1 

,或者更好的

var c0 = file.Value[0]; 
var match = file.Value.Skip(1).All(c => c.ata1 == c0.ata1 && ...); 
+0

我已經離開了當天的工作,但明天會試試這兩個。感謝您通過 –

+0

發送此快速勝利。謝謝你的幫助 –