在linq select中将yyyyMMdd转换为日期时间
本文关键字:日期 时间 转换 yyyyMMdd linq select 中将 | 更新日期: 2023-09-27 18:13:10
是否可以在linq select中将yyyyMMdd
字符串转换为日期时间?
1 -我试试Convert.toDateTime(), DateTime.ParseExact(), DateTime.Parse()
。它们都给我错误。
错误信息与此类似。
附加信息:LINQ to Entities不识别方法System。DateTime Parse(System.String)'方法,此方法不能转换为存储表达式。
2 -我可以确定这些数据需要转换为日期验证日期与yyyyMMdd
格式。
请看我下面的代码来理解我到底是什么意思。
return (from p in db.ExchangeDatas
where p.ExchangeDataSeqid == entity.ExchangeDataSeqid
select new ProcessAccountViewModel()
{
ExchangeCode = p.ExchangeCode,
UtilityCompany = p.UtilityCompanySeqid,
InvoiceBillingGroup = p.AccountBillingGroupSeqid,
AccountNumber = p.CurrentAccountNumber,
TurnOnDate = DateTime.ParseExact(p.AccountEffectiveTurnOn, "yyyyMMdd", CultureInfo.InvariantCulture),
SalesType = p.SalesType,
BillingCycle = p.BillingCycle,
TripNumber = p.TripNumber,
IsTimeOfDay = p.TODAccount == "Y" ? true : false,
IsExcessDistribution = p.ExcessDistributionAccount == "Y" ? true : false,
EnergyDeliverType = p.EnergyDeliveryType ?? 0,
Name = p.AccountName,
Address = p.AccountAddress,
Borough = p.Borough,
Facility = p.FacilitySeqid == null ? "" : p.FacilitySeqid.Value.ToString(),
Agency = p.AgencySeqid == null ? "" : p.AgencySeqid.Value.ToString(),
ServiceClass = p.DeliveryServiceClass,
AuthenticatedUserID = p.authenticatedUserID ?? 0,
ApprovedForCreation = p.ApprovedForCreation,
TransactionEffectiveDate = DateTime.ParseExact(p.TransactionEffectiveDate, "yyyyMMdd", CultureInfo.InvariantCulture),
ActivityTime = DateTime.ParseExact(p.ActivityTime, "yyyyMMdd", CultureInfo.InvariantCulture),
DateAdded = p.DateAdded,
LastUpdate = p.LastUpdate,
Exclude = p.Exclude,
IsProcessed = p.IsProcessed,
BillingPeriod = p.BillingPeriod
}).FirstOrDefault();
我不确定这是否会起作用,但尝试在您的ProcessAccountViewModel
中添加一些逻辑来为您处理这个问题。像这样:
class ProcessAccountViewModel()
{
...
DateTime TransactionEffectiveDate { get; set; } // you already have this
string TransactionEffectiveDateAsString // add this
{
set
{
TransactionEffectiveDate = DateTime.ParseExact(value,
"yyyyMMdd", CultureInfo.InvariantCulture);
}
}
}
然后,不要在LINQ查询中直接设置transactioneffecvedate,而是使用字符串版本:
所以,而不是:
TransactionEffectiveDate = DateTime.ParseExact(p.TransactionEffectiveDate,
"yyyyMMdd", CultureInfo.InvariantCulture),
TransactionEffectiveDateAsString = p.TransactionEffectiveDate,
LINQ to entities不支持DateTime。ParseExact方法,这样您就可以尝试使用AsEnumerable()将所需的集合带入内存,然后使用LINQ to Objects进行所有解析。
也许这能行?
var exchangeDatas = from p in db.ExchangeDatas
where p.ExchangeDataSeqid == entity.ExchangeDataSeqid
select p;
return (from p in exchangeDatas.AsEnumerable()
select new ProcessAccountViewModel()
{
ExchangeCode = p.ExchangeCode,
UtilityCompany = p.UtilityCompanySeqid,
InvoiceBillingGroup = p.AccountBillingGroupSeqid,
AccountNumber = p.CurrentAccountNumber,
TurnOnDate = DateTime.ParseExact(p.AccountEffectiveTurnOn, "yyyyMMdd", CultureInfo.InvariantCulture),
SalesType = p.SalesType,
BillingCycle = p.BillingCycle,
TripNumber = p.TripNumber,
IsTimeOfDay = p.TODAccount == "Y" ? true : false,
IsExcessDistribution = p.ExcessDistributionAccount == "Y" ? true : false,
EnergyDeliverType = p.EnergyDeliveryType ?? 0,
Name = p.AccountName,
Address = p.AccountAddress,
Borough = p.Borough,
Facility = p.FacilitySeqid == null ? "" : p.FacilitySeqid.Value.ToString(),
Agency = p.AgencySeqid == null ? "" : p.AgencySeqid.Value.ToString(),
ServiceClass = p.DeliveryServiceClass,
AuthenticatedUserID = p.authenticatedUserID ?? 0,
ApprovedForCreation = p.ApprovedForCreation,
TransactionEffectiveDate = DateTime.ParseExact(p.TransactionEffectiveDate, "yyyyMMdd", CultureInfo.InvariantCulture),
ActivityTime = DateTime.ParseExact(p.ActivityTime, "yyyyMMdd", CultureInfo.InvariantCulture),
DateAdded = p.DateAdded,
LastUpdate = p.LastUpdate,
Exclude = p.Exclude,
IsProcessed = p.IsProcessed,
BillingPeriod = p.BillingPeriod
}).FirstOrDefault();