我似乎無法找到我正在尋找的答案。希望這裏有人能幫忙。C#Linq或Lambda從類中獲取Dictionary <string,List <string>>
我有一個類包含一些進程的設置信息。每個類都有一個processId,taskId和其他各種我當前邏輯所不需要的信息。
public class ProcessSetting
{
public int ProcessId { get; set; }
public int TaskId { get; set; }
// Other properties not needed
}
存在多個ProcessSettings。我將數據拖入列表中。可能有一個與多個TaskIds關聯的processId。例如:
ProcessId: 1, TaskId: 1
ProcessId: 1, TaskId: 1
ProcessId: 1, TaskId: 2
ProcessId: 1, TaskId: 3
ProcessId: 2, TaskId: 3
ProcessId: 2, TaskId: 4
ProcessId: 3, TaskId: 1
我開始使用LINQ to只是收集,我從現有枚舉需要的值:(末尾使用不同的,以避免在多個記錄集的ProcessID 1和任務id 1的拉動)
var baseSettings = (from setting in processSettings
select new
{
ProcessStr = ((ProcessEnum)setting.ProcessId).ToString(),
TaskStr = ((TaskEnum)setting.TaskId).ToString()
}).Distinct();
這現在給我一個只有processId和taskId的列表。我在這裏發現了一些邏輯,使我朝着正確的方向前進,但並不完全是我所需要的。這是什麼:
Dictionary<string, List<string> = baseSettings.GroupBy(x => x.ProcessStr).ToDictionary(x => x.Key, x => x.ToList());
但是,這是不正確的。我得到一個錯誤:
"Cannot convert source type
System.Collections.Generic.Dictionary<string,System.Generic.List{ProcessStr:string, TaskStr:string}>>
to target typeSystem.Collections.Generic.Dictionary<string,System.Collections.Generic.List<string>>
我不想{ProcessStr,TaskStr}的價值的關鍵。任何人都可以將我指向正確的方向嗎?
對不起,聽不懂你在你的字典 –