0

私は次のLinqtoSqlを持っています:

var subscribers = (from s in dataContext.Employees
                               where s.RowType == "Edit"
                               select new SubscriberExportData
                                          {
                                              ID = s.ID.ToString(),
                                              GroupNum = s.GroupNumber,
                                              DivisionNum = s.DivisionNumber,
                                              HireDate = s.HireDate != null ? Convert.ToDateTime(s.HireDate).ToShortDateString() : string.Empty,
                                              EffDate = s.EffectiveDate != null ? Convert.ToDateTime(s.EffectiveDate).ToShortDateString() : string.Empty
                                           }

基本的に、日付値がnullでない場合は、短い日付形式に変換します。しかし、次のエラーが発生します。

System.InvalidOperationExceptionが処理されませんでしたMessage=式を変換できませんでした'Table(Employee).Where(s =>(s.RowType == "Edit"))。Select(s => new SubscriberExportData(){HireDate = IIF((s .HireDate!= null)、ToDateTime(Convert(s.HireDate))。ToShortDateString()、Invoke(value(System.Func`1 [System.String])))、EffDate = IIF((s.EffectiveDate!= null )、ToDateTime(Convert(s.EffectiveDate))。ToShortDateString)'をSQLに変換し、ローカル式として処理できませんでした。Source= System.Data.Linq

解決方法を教えてください。

4

1 に答える 1

0

クエリを2つに分割できます。1つはSQLが理解する操作で、もう1つはローカルで実行される文字列変換です。

var list = (from s in dataContext.Employees
                           where s.RowType == "Edit"
                           select new
                                      {
                                          s.ID 
                                          s.GroupNumber,
                                          s.DivisionNumber,
                                          s.HireDate 
                                          s.EffectiveDate  
                                       }).ToList();

var subscribers = (from s in list
                           select new SubscriberExportData
                                      {
                                          ID = s.ID.ToString(),
                                          GroupNum = s.GroupNumber,
                                          DivisionNum = s.DivisionNumber,
                                          HireDate = s.HireDate != null ? Convert.ToDateTime(s.HireDate).ToShortDateString() : string.Empty,
                                          EffDate = s.EffectiveDate != null ? Convert.ToDateTime(s.EffectiveDate).ToShortDateString() : string.Empty
                                       }
于 2012-09-12T19:15:48.397 に答える