LINQ使用List
本文关键字:成员 Object 使用 List LINQ | 更新日期: 2023-09-27 18:10:11
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来获取一个数据集并基于上述数据结构创建一个自定义对象类型,但该对象的一个成员是List数据类型。
我最初发现以下文章,但它不是完全工作,因为它是一个简单的字符串列表对象类型:使用Linq和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; }
}
可能是对lazyberezovsky的回答的一些调整
我相信你的错误来自你的dnFolders变量是一个列表?在这种情况下,您必须.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();
您的查询中有几个问题:
- 没有
section
变量定义 -
folder
类无folderItem
属性 - 您应该从每个组中选择
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()
};