0

我有以下 Linq to Sql:

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
                                           }

本质上,如果日期值不为空,则将它们转换为短日期格式。但我收到以下错误:

System.InvalidOperationException 未处理 Message=Could not translate expression '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

可以将查询一分为二,第一个是sql可以理解的操作,第二个是本地执行的字符串转换

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 回答