(Linq to SQL)为什么我的Concat失败了?

时间:2010-08-13 15:25:40

标签: c# linq-to-sql

我想要做的是搜索多个表(有些无关)并且每当找到匹配时 - 创建一个新的更通用的“结果”类型

表示每个表格 - 连接此结果类型集。我得到的错误是:使用UNION,INTERSECT或EXCEPT运算符组合的所有查询必须在其目标列表中具有相同数量的表达式。

有趣的是Concat适用于2张桌子,但第3张不起作用。这是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;
    }

和他们联系的代码

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);

据我所知,一切看起来完全相同,但在SQL分析器中,唯一的区别是没有“p5 AS [value2]”就像工作中的那样

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

如果你连接手机和电话,你会得到相同的结果吗?员工在一起?

我还会尝试从TelephonySearchResults中删除Type属性,我的直觉告诉我可能与此问题有关。