我正在開發 .NET CORE 6 應用程式,我Dictionary的結構為
Dictionary<string, Dictionary<int, string>> dataDictionary
我有基于List<Dictionary<int, string>> filteredDictionary以下的字典拉串列
var filteredDictionary = dataDictionary.Values.ToList();
每個串列有 17 個字典,表示AIMSchema每個屬性由 表示的類的屬性key (Int) no。所以我知道字典索引 0 存在 TransactionDate 的值,1 代表 MachineCode 等等...物件是我想將此字典串列轉換為List<AIMSchema>
我可以按如下方式回圈執行,但我想使用 LINQ
foreach (var schema in dataDictionary.Values)
{
if(schema != null)
{
var aimSchema = new AIMSchema
{
TransactionDate = schema[0],
MachineCode = schema[1],
// ... other properties
}
}
uj5u.com熱心網友回復:
好吧,我將準確地提供您所要求的答案。現有代碼的 LINQ 等效項如下所示:
List<AIMSchema> result = dataDictionary.Values
.Where(schema => schema != null)
.Select(schema => new AIMSchema
{
TransactionDate = schema[0],
MachineCode = schema[1],
// ... other properties
})
.ToList();
.Where是一個過濾器,相當于你的,if (schema != null)是.Select一個將條目變成AIMSchema物件的投影。由于我們dataDictionary.Values在此階段仍在處理查詢,因此.ToList()將結果具體化為List<AIMSchema>.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/487950.html
上一篇:Python僅當另一個字典的值為true時,才使用其中一個值屬于另一個串列的字典創建一個新串列
下一篇:如何從字串陣列創建物件映射?
