【发布时间】:2010-07-13 13:51:48
【问题描述】:
我从this question 开始,我在某种程度上回答了there,现在我在这里提出更基本的问题。我已将查询简化为:
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,一切正常。
但我确实希望格式化后的数字能很好地显示在我的列表中。作为最整洁、最干净的方式,您有什么建议?
【问题讨论】:
标签: c# linq linq-to-sql