CHV1 CDD1 CHV2 CDD2 CHV3 CDD3 CHV4 CDD4 SortCol SearchColCount
---- -------------------------------------------------- ---- -------------------------------------------------- ---- -------------------------------------------------- ----------- ------------------------------------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------------------------------------ --------------
Adventure Cafe Columbus Back Of The House Cook 624 Adams, Donald, 4180259, A A, Adams, Donald, 4180259 0
Adventure Cafe Columbus Back Of The House Cook 643 Conard, Virgil, 4180469, A A, Conard, Virgil, 4180469 0
Adventure Cafe Columbus Back Of The House Cook 629 Pheiffer, Seth, 4180373, A A, Pheiffer, Seth, 4180373 0
Adventure Cafe Columbus Back Of The House Cook 645 Sees, Patrick, 4180474, A A, Sees, Patrick, 4180474 0
Adventure Cafe Columbus Back Of The House Cook 657 Walter, Derek, 4180508, A A, Walter, Derek, 4180508 0
以上是來自DB的數據集。LINQ自定義對象列表<Object>成員
我想使用LINQ獲取數據集並基於上述數據結構創建自定義對象類型,但該對象的成員之一是List數據類型。
我最初發現下面的文章,但它不工作準確,因爲它是一個簡單的字符串列表對象類型: Convert Datatable to Object with Linq and Group by 這是給我下面的錯誤:
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.List'. An explicit conversion exists (are you missing a cast?)
我列出我對上述引用的文章中的代碼進行了修改。理想情況下,我希望能夠基於CDD2,CDD3,CDD4等動態地創建多個「folderItem」列表...... CDD列的數量是可變的,這進一步複雜化了。
這些數據最終將作爲JSON從WCF服務中返回。
dnFolders = from row in dnDataTable.AsEnumerable()
group row by new
{
id = row.Field<string>(1),
value = row.Field<string>(2)
} into folder
select new folder
{
id = folder.Key.id,
value = folder.Key.value
folderItem = section.Select(r=>r.Field<String>(3)).ToList()
};
文件夾類的定義:
public class folder
{
[DataMember(Name = "id", Order = 1)]
public string id { get; set; }
[DataMember(Name = "value", Order = 2)]
public string value { get; set; }
[DataMember(Name = "type", Order = 3)]
public string type { get; set; }
[DataMember(Name = "sortCol", Order = 4)]
public string sortCol { get; set; }
[DataMember(Name = "folderItems", Order = 5)]
public List<folderItem> folderItems { get; set; }
}
folderItem類定義
public class folderItem
{
[DataMember(Name = "value", Order = 1)]
public string value { get; set; }
[DataMember(Name = "id", Order = 2)]
public string id { get; set; }
[DataMember(Name = "type", Order = 2)]
public string type { get; set; }
}
這個工作,但我仍然在努力確定代碼的方式被寫入基礎上添加了「folderItems」的動態數數據集中表示的列數,因爲它也是動態的。 @ andreas-johansson - 可能嗎? – user2300514 2013-04-23 18:00:55
我不明白這個問題。 – 2013-04-23 18:40:13
@ user2300514這不能工作 – 2013-04-25 17:45:33