我想將這個串列合并或合并為一個串列。如何在沒有錯誤的情況下連接此串列?
編譯器錯誤訊息:CS0266:無法將型別 'System.Collections.Generic.IEnumerable<System.Collections.Generic.Dictionary<string,object>>' 隱式轉換為 'System.Collections.Generic.List<System.Collections.Generic.Dictionary <字串,物件>>'。存在顯式轉換(您是否缺少演員表?)
List<Dictionary<string, object>> result1 = process1(); // OK
List<Dictionary<string, object>> result2 = process2(); // OK
List<Dictionary<string, object>> result3 = process3(); // OK
List<Dictionary<string, object>> result4 = process4(); // OK
List<Dictionary<string, object>> result5 = process5(); // OK
var d1 = result1;
if(result2 != null){
d1 = d1.Concat(result2).ToList();
}
if(result3 != null){
d1 = d1.Concat(result3);
}
if(result4 != null){
d1 = d1.Concat(result4);
}
if(result5 != null){
d1 = d1.Concat(result5);
}
uj5u.com熱心網友回復:
問題是那d1是 aList<>但你有一個Dictionary<>. 您需要添加ToList到每一行
但是如果你把所有的鏈接Concat()在一起,你只需要ToList()在最后:
var empty = Enumerable.Empty<Dictionary<string, object>>()
var d1 = result1 ?? empty
.Concat(result2 ?? empty)
.Concat(result3 ?? empty)
.Concat(result4 ?? empty)
.Concat(result5 ?? empty)
.ToList();
uj5u.com熱心網友回復:
您不僅應該將 ToList() 添加到:
d1 = d1.Concat(result2).ToList();
還有下一個連接:
if(result3 != null){
d1 = d1.Concat(result3).ToList();
} etc..
uj5u.com熱心網友回復:
Concat 是一個很好的運算子,但我認為如果你使用d1.AddRange(result2);etc會更好。
您得到的錯誤是因為 Concat 回傳 anIEnumerable<T>而 AddRange 運算子回傳 a List<T>。
這樣,您就可以防止ToList()操作員進行不必要的強制轉換。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/410017.html
標籤:
上一篇:根據列鍵合并字典中的dfs
