У меня довольно сложный запрос Linq:
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 раза, то есть для трех разных типов телефонных номеров.
Согласно документации к SQL-серверу, это происходит из-за некорректного запроса. Но это Линк, ради всего святого! Как это может быть неправильный запрос?
Помимо основного ответа, я также буду признателен за любые ваши комментарии по поводу оптимизации моего запроса ...
Спасибо!