我有以下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
}
基本上,如果日期值不为null,则将它们转换为短日期格式。但我收到以下错误:
System.InvalidOperationException未处理 Message =无法翻译表达式'Table(Employee).Where(s =>(s.RowType ==“Edit”))。选择(s => new SubscriberExportData(){HireDate = IIF((s.HireDate! = null),ToDateTime(转换(s.HireDate))。ToShortDateString(),Invoke(值(System.Func`1 [System.String]))),EffDate = IIF((s.EffectiveDate!= null),ToDateTime (转换(s.EffectiveDate))。ToShortDateString)'进入SQL并且不能将其视为本地表达式。 源= System.Data.Linq程序
请告诉我如何解决。
答案 0 :(得分: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
}