LINQ to List <object> member </object>的自定义对象

时间:2013-04-20 09:56:17

标签: c# json linq

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

以上是数据库中的数据集。

我正在尝试使用LINQ获取数据集并根据上述数据结构创建自定义对象类型,但该对象的其中一个成员是List数据类型。

我最初发现了以下文章,但它并没有完全正常工作,因为它是一个简单的字符串List对象类型: Convert Datatable to Object with Linq and Group by 这给了我以下错误:

  

无法隐式转换类型   'System.Collections.Generic.IEnumerable'   到'System.Collections.Generic.List'。   存在显式转换(您是否错过了演员?)

我列出了我对上面引用文章中的代码的改编。理想情况下,我希望能够基于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; }
}

2 个答案:

答案 0 :(得分:1)

也许lazyberezovsky的回答有些调整

我相信你的错误来自你的dnFolders varable是一个List? 在这种情况下,您必须.ToList()整个查询

dnFolders = 
    (from row in dnDataTable.AsEnumerable()
    group row by new
    {
        id = row.Field<string>(1),
        value = row.Field<string>(2)
    } into g
    select new folder
    {
        id = g.Key.id,
        value = g.Key.value
        folderItems = g.Select(r => r.Field<FolderItems>(5) })
                       .ToList()
    }).ToList();

答案 1 :(得分:0)

您的查询中有几个问题:

  • 没有定义section变量
  • folderItem
  • 中没有folder属性
  • 您应该从每个组中选择folderItem个对象的列表,但是您选择了字符串列表

以下是您的查询应该如何(我不知道应该转到folderItem对象的内容,因此仅使用type属性进行初始化):

dnFolders = 
    from row in dnDataTable.AsEnumerable()
    group row by new
    {
        id = row.Field<string>(1),
        value = row.Field<string>(2)
    } into g
    select new folder
    {
        id = g.Key.id,
        value = g.Key.value
        folderItems = g.Select(r => new folderItem { type = r.Field<string>(3) })
                       .ToList()
    };