gpt4 book ai didi

c# - 循环 System.Data.Objects.DataClasses.EntityCollection

转载 作者:行者123 更新时间:2023-11-30 17:13:05 25 4
gpt4 key购买 nike

我正在开发 WCF 网络服务,我使用 ADO.Net 实体访问数据库。

我使用此方法将一些数据库对象转换为契约(Contract)对象:

public static List<BlockContract> FormBlocks(int formId)
{
List<BlockContract> blocksList = null;

using (var vAdmEntities = new ADMDatabase.ADMEntities())
{
var blockEntities = from f in vAdmEntities.Form
where f.formId == formId
select f.Block;
if (blockEntities != null)
{
blocksList = new List<BlockContract>(blockEntities.Count());
foreach (ADMDatabase.Block b in blockEntities)
{
BlockContract blockContract = new BlockContract
{
blockId = b.blockId,
name = b.name,
orderInForm = b.orderInForm
};
blocksList.Add(blockContract);
}
}
}

return blocksList;
}

在这里,foreach (ADMDatabase.Block b in blockEntities),我得到一个错误:

Cannot convert type 'System.Data.Objects.DataClasses.EntityCollection<ADMDatabase.Block>' to 'ADMDatabase.Block'

我该如何解决这个错误?

最佳答案

你需要改变你的循环;

foreach (var blockItems in blockEntities){
foreach (ADMDatabase.Block b in blockItems)
{
BlockContract blockContract = new BlockContract
{
blockId = b.blockId,
name = b.name,
orderInForm = b.orderInForm
};
blocksList.Add(blockContract);
}
}

您正在选择具有以下内容的 block 实体;

var blockEntities = from f in vAdmEntities.Form
where f.formId == formId
select f.Block;

在这里,如果您调试 blockEntities,您将得到类似于列表的列表,因为 Form.Block 是 block 项目的列表。

关于c# - 循环 System.Data.Objects.DataClasses.EntityCollection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10086928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com