2010-08-13 58 views
0

什麼即時試圖做的是搜索直通多個表(即是那種無關),每次它找到一個匹配 - 創建一個新的更通用的「結果」類型(Linq to SQL)爲什麼我的Concat失敗?

的everytable - CONCAT這個結果類型集。我得到的錯誤是:所有使用UNION,INTERSECT或EXCEPT運算符組合的查詢在其目標列表中必須具有相同數量的表達式。

有趣的是Concat在兩張桌子上工作,但第三個不起作用。這裏是SQL輪廓

工作

exec sp_executesql N'SELECT [t2].[Id], [t2].[value] AS [Type], [t2].[Name] AS  [Description], [t2].[value2] AS [Action] 
FROM (
SELECT [t0].[Id], @p1 AS [value], [t0].[Name], @p2 AS [value2] 
FROM [dbo].[PLID] AS [t0] 
WHERE [t0].[Name] LIKE @p0 
UNION ALL 
SELECT [t1].[Id], @p4 AS [value], [t1].[Number], @p5 AS [value2] 
FROM [dbo].[Phone] AS [t1] 
WHERE [t1].[Number] LIKE @p3 
) AS [t2]',N'@p0 nvarchar(6),@p1 nvarchar(4),@p2 nvarchar(5),@p3 nvarchar(6),@p4 nvarchar(9),@p5 nvarchar(5)', @p0=N'%2010%',@p1=N'PLID',@p2=N'Phone',@p3=N'%2010%',@p4=N'Telephone',@p5=N'Phone' 

不工作

exec sp_executesql N'SELECT [t2].[Id], [t2].[value] AS [Type], [t2].[Name] AS [Description], [t2].[value2] AS [Action] 
FROM (
SELECT [t0].[Id], @p1 AS [value], [t0].[Name], @p2 AS [value2] 
FROM [dbo].[PLID] AS [t0] 
WHERE [t0].[Name] LIKE @p0 
UNION ALL 
SELECT [t1].[Id], @p4 AS [value], [t1].[LastName] 
FROM [dbo].[Employee] AS [t1] 
WHERE [t1].[LastName] LIKE @p3 
) AS [t2]',N'@p0 nvarchar(6),@p1 nvarchar(4),@p2 nvarchar(5),@p3 nvarchar(6),@p4 nvarchar(8)',@p0=N'%2010%',@p1=N'PLID',@p2=N'Phone',@p3=N'%2010%',@p4=N'Employee' 

下面是實際的C#代碼(工作)

public IQueryable<TelephonySearchResults> SearchPhones(string search) 
    { 
     IQueryable<TelephonySearchResults> results = from phone in _dataContext.Phones 
                where phone.Number.Contains(search) 
                select new TelephonySearchResults 
                   { 
                    Description = phone.Number, 
                    Type = SearchTelephonyResultsType.Phone, 
                    Id = phone.Id, 
                    Action = "Phone" 
                   }; 


     return results; 
    } 

    public IQueryable<TelephonySearchResults> SearchPlids(string search) 
    { 
     IQueryable<TelephonySearchResults> results = from plid in _dataContext.PLIDs 
                where plid.Name.Contains(search) 
                select new TelephonySearchResults 
                   { 
                    Description = plid.Name, 
                    Type = SearchTelephonyResultsType.Plid, 
                    Id = plid.Id, 
                    Action = "Phone" 
                   }; 


     return results; 
    } 

不工作

public IQueryable<TelephonySearchResults> SearchEmployees(string search) 
    { 

     IQueryable<TelephonySearchResults> results = from employee in _dataContext.Employees 
                where employee.LastName.Contains(search) 
                select new TelephonySearchResults 
                { 
                 Description = employee.LastName, 
                 Type = SearchTelephonyResultsType.Employee, 
                 Id = employee.Id, 
                 Action = "Employee" 
                }; 

     return results; 
    } 

這concats他們

IQueryable<TelephonySearchResults> phones = _repository.SearchPhones(searchCriteria); 
     IQueryable<TelephonySearchResults> plids = _repository.SearchPlids(searchCriteria); 
     IQueryable<TelephonySearchResults> employees = _repository.SearchEmployees(searchCriteria); 

//works 
     IQueryable<TelephonySearchResults> results = plids.Concat(phones); 
//does not 
IQueryable<TelephonySearchResults> results = plids.Concat(employees); 

至於我可以告訴一切看起來一模一樣codewise,但在SQL事件探查,唯一的區別是沒有「代碼p5 AS [value2]「就像在工作的人一樣

任何幫助將不勝感激。

回答

0

如果你連接電話&員工在一起,你會得到同樣的結果嗎?

我也會嘗試從TelephonySearchResults中消除Type屬性,我的直覺告訴我可能與該問題有關。