1

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);
3
  • Is it an option to just not set that property and let it be null, or whatever other default value it would have? Commented Oct 21, 2013 at 19:23
  • I've added workItemQuery. There is also some filtering after, but in this context, it's not really important. Commented Oct 21, 2013 at 19:36
  • @Servy no I can't when I do that, I received another error message: "The type 'WorkItemAssignment' appears in two structurally incompatible initializations within a single LINQ to Entities query. A type can be initialized in two places in the same query, but only if the same properties are set in both places and those properties are set in the same order." Commented Oct 21, 2013 at 19:37

1 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

4 Comments

I can't I really need to have my IQueryable because my kendo grid is doing the filtering, and I don't want to generate the queries until the filtering is done
And I do understand that the provider doesn't know what is "Enumerable.Empty<T>" I can read the error message. I just need another way to do an empty list to force the union of the 2 queries
it did change the error message. but it still doesn't work. When I remove the properties "Specialities" from my 2 query, it works... here is the new message: "The 'Distinct' operation cannot be applied to the collection ResultType of the specified argument. Parameter name: argument"
Finally it works, I just add to create a new genericEqualityComparer. And removed my collection from being compared.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.