我有一個1到很多關係在tblBusiness
和tblPhone
之間。對於特定的BusinessID
我試圖返回單個字符串信息綁定到文本框。以下是我的最新嘗試以及來自同一LINQ的生成的SQL。無論我嘗試過什麼,它都會返回NULL
,除非所有3個字段都有值。LINQ 2 SQL返回多個爲單個字符串
我在做什麼錯?謝謝!
首先LINQ:
using (var context = ConnectDataContext.Create())
{
context.Log = Console.Out;
var business = from businesse in context.tblBusinesses
where businesse.BusinessID == businessID
select businesse.BusinessName
+ businesse.ContactName
+ businesse.tblPhones.Select(p=>p.PhoneNumber)
.FirstOrDefault()
?? string.Empty;
return business.Single();
}
現在SQL:
SELECT [t2].[value]
FROM (
SELECT COALESCE(([t0].[BusinessName] + [t0].[ContactName]) + ((
SELECT TOP (1) [t1].[PhoneNumber]
FROM [dbo].[tblPhone] AS [t1]
WHERE [t1].[BusinessID] = [t0].[BusinessID]
)),@p0) AS [value], [t0].[BusinessID]
FROM [dbo].[tblBusiness] AS [t0]
) AS [t2]
WHERE [t2].[BusinessID] = @p1
-- @p0: Input NVarChar (Size = 1; Prec = 0; Scale = 0) [ ]
-- @p1: Input Int (Size = 0; Prec = 0; Scale = 0) [118]