2016-10-14 25 views
0

yyyyMMddの文字列をlinq selectの日付時刻に変換する方法ですか?yyyyMMddをlinq selectの日付時刻に変換する

1 - 私は試してみますConvert.toDateTime(), DateTime.ParseExact(), DateTime.Parse()です。それらのすべてが私に誤りを与える。

エラーメッセージはこれに似ています。

追加情報:LINQ to Entitiesは、 'System.DateTime Parse(System.String)'メソッドを認識せず、このメソッドをストア式に変換することはできません。

2 - これらのデータをyyyyMMdd形式の日付の有効期限に変換する必要があることを確認できます。

正確に何を意味するのかを理解するには、次のコードをご覧ください。あなたはAsEnumerable()を使用してメモリに必要なコレクションを持参しようとした後、オブジェクトにLINQを使用して、すべての構文解析を行うことができますので、enititesへ

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(); 
+1

実行後に日付を書式設定する必要があります。同等の機能がないため、LINQは 'ParseExact'をSQLに変換できません – DGibbs

+0

ProcessAccountViewModelクラスでは、これらはDateTimeプロパティとして定義されていますか? – jdweng

+0

@jdwengはいそうです。 – user3754008

答えて

0

LINQはDateTime.ParseExactメソッドをサポートしていません。

これはうまくいくでしょうか?

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(); 
+0

はい、この作品。ありがとうございました。 – user3754008

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, 
+0

これが動作しないうちに試してみます。 – user3754008

関連する問題