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
The above is the dataset from the DB.
I am trying to use LINQ to take a dataset and create a custom object type, based on the above data structure, but one of the members of that object is a List data type.
I originally found the following article, but it wasn't working exactly, since it was a simple string List object type:
Convert Datatable to Object with Linq and Group by
This is giving me the following error:
Cannot implicitly convert type
'System.Collections.Generic.IEnumerable'
to 'System.Collections.Generic.List'.
An explicit conversion exists (are you missing a cast?)
I have listed my adaptation to the code from the above referenced article. Ideally, I would like to be able to dynamically create multiple "folderItem" Lists based on CDD2, CDD3, CDD4, etc... The number of CDD columns would be variable, which further complicated things.
This data will ultimately be return from a WCF service as JSON.
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()
};
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; }
}