2009-08-11 77 views
2

我有一个相当复杂的Linq查询:“子查询未使用EXISTS引入时,只能在选择列表中指定一个表达式。”

var q = from eiods in LinqUtils.GetTable<EIOfficialDesignee>() 
     let eiodent = eiods.Entity 
     join tel in LinqUtils.GetTable<EntityTelephone>() 
     on new { EntityID = eiodent.ID, TelephoneType = EntityTelephone.TTelephone.Office } equals new { tel.EntityID, tel.TelephoneType } 
     into Tel 
     let Tel1 = Tel.FirstOrDefault() 
     join fax in LinqUtils.GetTable<EntityTelephone>() 
     on new { EntityID = eiodent.ID, TelephoneType = EntityTelephone.TTelephone.Fax } equals new { fax.EntityID, fax.TelephoneType } 
     into Fax 
     let Fax1 = Fax.FirstOrDefault() 
     join cell in LinqUtils.GetTable<EntityTelephone>().DefaultIfEmpty() 
     on new { EntityID = eiodent.ID, TelephoneType = EntityTelephone.TTelephone.Mobile } equals new { cell.EntityID, cell.TelephoneType } 
     into Mobile 
     let Mobile1 = Mobile.FirstOrDefault() 
     where eiods.ID == CurrentEIPatient.EIOfficialDesigneeID 
     select new { 
      ID = eiods.ID, 
      EIODName = eiodent.FormattedName, 
      Phone = Tel1 != null ? Tel1.FormattedNumber : "", 
      Fax = Fax1 != null ? Fax1.FormattedNumber : "", 
      Cellphone = Mobile1 != null ? Mobile1.FormattedNumber : "", 
     }; 

该查询返回我的SQL错误:

Only one expression can be specified in the select list when the subquery is not introduced with EXISTS 
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS 
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS 

是,一式三份。这是一个明显的指示,即查询问题重复了3次,即3种不同类型的电话号码。

根据SQL服务器文档,这来自格式错误的查询。但是这是Linq,看在上帝的份上!它如何能够恶化查询?

从主答案

除此之外,我也很感激您的任何意见如何优化我的查询......

谢谢!

回答

2

解决它自己,这里是为了任何人的利益。

魔鬼末尾的选择子句中,具体有:

Phone = Tel1 != null ? Tel1.FormattedNumber : "", 
Fax = Fax1 != null ? Fax1.FormattedNumber : "", 
Cellphone = Mobile1 != null ? Mobile1.FormattedNumber : "", 

即FormattedNumber属性是基于所述EntityTelephone对象的NumberExtension特性计算的字段。当我用Number替换FormattedNumber时,一切正常。

发现此问题的最佳解决方案here

相关问题