سؤال

I want to union two list in entity framework. In one of the union part, I have a collection and in the other one, the collection is empty. I'm trying to put the collection to empty but it doesn't work...

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

error message:

LINQ to Entities does not recognize the method 'System.Collections.Generic.IEnumerable`1[System.String] EmptyString' method, and this method cannot be translated into a store expression.

When I remove the properties "Specialities" from my 2 queries, it works...

EDIT:

Because of the first answer, I want to clarify that I really need to stay in the entity context to keep my IQueryable without having my list materialize. My Kendo grid is doing some filtering / paging, and I want to send it a IQueryable.

UPDATE 2:

After using the solution of @Moho, It was only missing an IEqualityComparer that removed my collection from being compared between the 2 queries.

return assigments.Union(workItemAssignments ,assignmentEqualityComparer);
هل كانت مفيدة؟

المحلول

The Linq to Entities provider doesn't know what to do with Enumerable.Empty<T>() when it tries to convert your query to SQL.

Update:

I believe this should work:

Specialties = new string[]{}.AsQueryable()
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top