我有一组可以添加到数据库的可相关实体,但似乎需要进行一些转换.谁能指出我正确的方向?bool InsertDetails(DataTable detailTable, string fileName){using (SunseapEBTContext context = new SunseapEBTContext()...

我有一组可以添加到数据库的可相关实体,但似乎需要进行一些转换.谁能指出我正确的方向?
bool InsertDetails(DataTable detailTable, string fileName)
{
using (SunseapEBTContext context = new SunseapEBTContext())
{
if (InsertMaster(fileName))//if creating master record successful
{
int masterId = GetSPReadingM(m => m.FileName == fileName).SPReadingMasterId; //get MasterID of file uploaded
var details = detailTable.AsEnumerable().Select(row => new LeasingSPReadingDetailEntity()//new entity from datatable
{
//SPReadingId = row.Field<long>("ProductID"),
SPReadingMasterId = masterId,
BillCycleYear = int.Parse(row.Field<int>("Bill Cycle").ToString().Substring(0, 4)),
BillCycleMonth = byte.Parse(row.Field<byte>("Bill Cycle").ToString().Substring(4))
});
foreach(IEnumerable<LeasingSPReadingDetailEntity> detail in details)
{
context.LeasingSPReadingDetailEntities.AddObject(detail);
}
context.SaveChanges();
}
return true;
}
}
在foreach循环中,抛出异常
CS1503 Argument 1: cannot convert from ‘System.Collections.Generic.IEnumerable’ to ‘SunseapEBT.Domain.BillingModule.LeasingContract.Entity.LeasingSPReadingDetailEntity’
LeasingSPReadingDetailEntity类:
public class LeasingSPReadingDetailEntity
{
public long SPReadingId { get; set; }
public int SPReadingMasterId { get; set; }
public int BillCycleYear { get; set; }
public byte BillCycleMonth { get; set; }
}
更多信息:
将上载包含详细信息的文件,并在一个表中创建主记录.文件中的详细信息将使用第一个表中自动生成的masterId添加到单独的表中.问题是无法将详细信息添加到数据库中.
编辑:
我发现了为什么会出错.该错误是由于文件的内容.某些行没有输入值,最后一行没有遵循其余行的格式,因为它显示了总行数.谢谢大家的帮助!
解决方法:
@slawekwin评论是答案.但我认为有一个更好的解决方案,因为看起来你的代码迭代2x:1st生成新的Enumerable(浪费内存),2nd将对象添加到上下文.
当您迭代每一行时,也可以直接添加对象.
foreach(var row in detailTable.AsEnumerable())
{
context.LeasingSPReadingDetailEntities.AddObject(
new LeasingSPReadingDetailEntity()//new entity from datatable
{
//SPReadingId = row.Field<long>("ProductID"),
SPReadingMasterId = masterId,
BillCycleYear = int.Parse(row.Field<string>("Bill Cycle").Substring(0, 4)),
BillCycleMonth = byte.Parse(row.Field<string>("Bill Cycle").Substring(4)),
AccountNumber = row.Field<string>("Account No."),
PeriodStart = row.Field<DateTime>("Period Start"),
PeriodEnd = row.Field<DateTime>("Period End"),
TownCouncil = row.Field<string>("Customer Name"),
Service = row.Field<string>("Service Type"),
Adjustment = row.Field<string>("Adjustment"),
Block = row.Field<string>("Blk"),
AddressLine1 = row.Field<string>("Adress Line 1"),
AddressLine2 = row.Field<string>("Adress Line 2"),
AddressLine3 = row.Field<string>("Postal Code"),
Usage = row.Field<decimal>("Usage"),
Rate = row.Field<decimal>("Rate"),
Amount = row.Field<decimal>("Amount")
}
);
}
—编辑—
我不确定,但我可以猜测“Bill Cycle”字段既不是int也不是byte.因此,您应该将其作为字符串检索,然后将其解析为新对象.这是我改变的部分:
BillCycleYear = int.Parse(row.Field<string>("Bill Cycle").Substring(0, 4)),
BillCycleMonth = byte.Parse(row.Field<string>("Bill Cycle").Substring(4)),
本文标题为:c# – 如何从IEnumerable集合中将对象添加到数据库?


- C#实现猜数字游戏 2023-02-08
- c#获取客户端IP地址(考虑代理) 2023-02-07
- C# WPF实现动态3D光照效果 2023-07-18
- C#使用Lazy<T>实现对客户订单的延迟加载 2023-07-04
- Unity实现虚拟键盘 2023-04-09
- C#之Socket客户端全过程 2023-07-19
- 原生实现C#与Lua相互调用方法(Unity3D可用) 2023-05-31
- Unity使用DoTween实现抛物线效果 2023-04-21
- ASP.NET Core部署到CentOS7,使用Nginx代理 2023-09-27
- C#中Equals和GetHashCode使用及区别 2023-02-08