【问题标题】:How to format data value如何格式化数据值
【发布时间】:2012-09-13 03:15:48
【问题描述】:

我有以下 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=无法翻译表达式'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

请告诉我如何解决它。

【问题讨论】:

  • 您使用的是哪种语言,VB 还是 C#?

标签: linq


【解决方案1】:

您可以将查询一分为二,第一个是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
                                       }

【讨论】:

    猜你喜欢
    • 2022-11-29
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 2021-09-20
    • 1970-01-01
    • 1970-01-01
    • 2021-09-30
    • 1970-01-01
    相关资源
    最近更新 更多