使用Entity框架创建Union失败

时间:2013-10-21 19:19:00

标签: c# linq entity-framework iqueryable

我想在实体框架中结合两个列表。在其中一个联合部分,我有一个集合,而在另一个,集合是空的。我试图把这个集合变成空的但它不起作用......

var query = Context.Assignments.AsQueryable();
var workItemQuery = Context.WorkItems.AsQueryable();

var assigments = query.Select(o => new WorkItemAssignment()
            {
                EndDate = o.WorkItem.EndDate,
                StartDate = o.WorkItem.StartDate,
                IsExternal = o.Resource.IsExternalEmp ? Resources.External : Resources.Internal,
                ResourceAssignedName = o.Resource.FirstName + " " + o.Resource.LastName,
                RoleName = o.Role.Name,
                Specialties = o.AssignmentSpecialties.Select(a => a.Specialty.Name),
                WorkItemName = o.WorkItem.Name,
                WorkItemOwner = o.WorkItem.OwnerResource.FirstName + " " + o.WorkItem.OwnerResource.LastName,
                WorkItemStatus = o.WorkItem.WorkItemStatus.Name,
                Days = o.Days.Value,
                Percentage = o.Percentage.Value,
                RequestId = o.WorkRequestAllocationId != null && o.WorkRequestAllocationId != Guid.Empty ? o.WorkRequestAllocation.WorkRequest.RequestId : (int?) null
            });

var  workItemAssignments = workItemQuery.Select(o => new WorkItemAssignment()
            {
                EndDate = o.EndDate,
                StartDate = o.StartDate,
                IsExternal = "N/A",
                ResourceAssignedName = "N/A",
                RoleName = "N/A",
                RoleProficiency = "N/A",
                Specialties = Enumerable.Empty<string>().AsQueryable(), //DO NOT WORK !!!
                WorkItemName = o.Name,
                WorkItemOwner = o.OwnerResource.FirstName + " " + o.OwnerResource.LastName,
                WorkItemStatus = o.WorkItemStatus.Name,
                Days = null,
                Percentage = null,
                RequestId = null
            });
return assigments.Union(workItemAssignments);

错误消息:

  

LINQ to Entities无法识别该方法   “System.Collections.Generic.IEnumerable`1 [System.String]   EmptyString'方法,这个方法无法翻译成   商店表达。

当我从我的2个查询中删除属性“Specialties”时,它可以正常工作......

编辑:

由于第一个答案,我想澄清一点,我确实需要留在实体上下文中,以保持我的IQueryable而不会让我的列表成为现实。我的Kendo网格正在进行一些过滤/分页,我想发送一个IQueryable

更新2:

在使用@Moho的解决方案之后,它只缺少一个IEqualityComparer来删除我的集合,使其无法在2个查询之间进行比较。

return assigments.Union(workItemAssignments ,assignmentEqualityComparer);

1 个答案:

答案 0 :(得分:1)

Linq to Entities提供程序在尝试将您的查询转换为SQL时不知道如何处理Enumerable.Empty<T>()

更新

我相信这应该有效:

Specialties = new string[]{}.AsQueryable()