c# - LINQ to custom objects with List<Object> member -
chv1 cdd1 chv2 cdd2 chv3 cdd3 chv4 cdd4 sortcol searchcolcount ---- -------------------------------------------------- ---- -------------------------------------------------- ---- -------------------------------------------------- ----------- ------------------------------------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------------------------------------ -------------- adventure cafe columbus of house cook 624 adams, donald, 4180259, a, adams, donald, 4180259 0 adventure cafe columbus of house cook 643 conard, virgil, 4180469, a, conard, virgil, 4180469 0 adventure cafe columbus of house cook 629 pheiffer, seth, 4180373, a, pheiffer, seth, 4180373 0 adventure cafe columbus of house cook 645 sees, patrick, 4180474, a, sees, patrick, 4180474 0 adventure cafe columbus of house cook 657 walter, derek, 4180508, a, walter, derek, 4180508 0
the above dataset db.
i trying use linq take dataset , create custom object type, based on above data structure, 1 of members of object list data type.
i found following article, wasn't working exactly, since simple string list object type: convert datatable object linq , group by giving me following error:
cannot implicitly convert type 'system.collections.generic.ienumerable' 'system.collections.generic.list'. explicit conversion exists (are missing cast?)
i have listed adaptation code above referenced article. ideally, able dynamically create multiple "folderitem" lists based on cdd2, cdd3, cdd4, etc... number of cdd columns variable, further complicated things.
this data return wcf service json.
dnfolders = row in dndatatable.asenumerable() group row new { id = row.field<string>(1), value = row.field<string>(2) } folder select new folder { id = folder.key.id, value = folder.key.value folderitem = section.select(r=>r.field<string>(3)).tolist() };
folder class definition:
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 class definition
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; } }
perhaps adjustments lazyberezovsky's answer
i believe error comes dnfolders varable list ? in case must .tolist() entire query
dnfolders = (from row in dndatatable.asenumerable() group row new { id = row.field<string>(1), value = row.field<string>(2) } g select new folder { id = g.key.id, value = g.key.value folderitems = g.select(r => r.field<folderitems>(5) }) .tolist() }).tolist();
Comments
Post a Comment