将yyyyMMdd转换为linq select中的日期时间

时间:2016-10-14 15:46:33

标签: c# .net linq type-conversion

是否可以在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();

2 个答案:

答案 0 :(得分:1)

我不确定这是否有效,但请尝试在ProcessAccountViewModel中添加一些逻辑来处理这个问题。像这样:

class ProcessAccountViewModel()
{
   ...
   DateTime TransactionEffectiveDate { get; set; } // you already have this
   string TransactionEffectiveDateAsString // add this
   {
       set
       {
           TransactionEffectiveDate = DateTime.ParseExact(value,
                      "yyyyMMdd", CultureInfo.InvariantCulture);
       }
   }
}

然后,不要直接在LINQ查询中设置TransactionEffectiveDate,而是使用字符串版本:

所以,而不是:

TransactionEffectiveDate = DateTime.ParseExact(p.TransactionEffectiveDate,
                                "yyyyMMdd", CultureInfo.InvariantCulture),

待办事项

TransactionEffectiveDateAsString = p.TransactionEffectiveDate,

答案 1 :(得分:0)

LINQ to enitites不支持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();