我从this question开始,我回答了there,现在我在这里提出更基本的问题。我已经简化了查询到这一点:Linq“无法将表达式...转换为SQL,无法将其视为本地表达式。”
var q = from ent in LinqUtils.GetTable<Entity>()
from tel in ent.Telephones.DefaultIfEmpty()
select new {
Name = ent.FormattedName,
Tel = tel != null ? tel.FormattedNumber : "" // this is what causes the error
};
tel.FormattedNumber
是结合了Number
和Extension
领域成整齐的格式化字符串的属性。下面是导致错误:
System.InvalidOperationException: Could not translate expression 'Table(Entity).SelectMany(ent => ent.Telephones.DefaultIfEmpty(), (ent, tel) => new <>f__AnonymousType0`2(Name = ent.FormattedName, Tel = IIF((tel != null), tel.FormattedNumber, "")))' into SQL and could not treat it as a local expression.
如果我改变上述从FormattedNumber
到只是简单的Number
,一切正常参考。
但我确实希望格式化的数字能够很好地显示在我的列表中。作为最干净最干净的方式,你推荐什么?