1

我想在实体框架中合并两个列表。在一个联合部分,我有一个集合,而在另一个集合中,集合是空的。我正在尝试将集合清空,但它不起作用...

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 个查询中删除属性“Specialities”时,它可以工作......

编辑:

由于第一个答案,我想澄清一下,我确实需要留在实体上下文中以保持我IQueryable的列表没有实现。我的剑道网格正在做一些过滤/分页,我想给它发送一个IQueryable.

更新 2:

使用@Moho 的解决方案后,它只是缺少一个IEqualityComparer将我的集合从两个查询之间的比较中删除。

return assigments.Union(workItemAssignments ,assignmentEqualityComparer);
4

1 回答 1

1

Enumerable.Empty<T>()Linq to Entities 提供程序在尝试将您的查询转换为 SQL 时 不知道该怎么做。

更新:

我相信这应该有效:

Specialties = new string[]{}.AsQueryable()
于 2013-10-21T19:24:46.353 回答