user547794
user547794

Reputation: 14511

Concatenating two ViewModels

I have two instances of the same ViewModel that I would like to concatenate:

var queryNew = from a in ICDUnitOfWork.AlphaGroups.Find()
                           join e in ICDUnitOfWork.Alphas.Find()
                               on a.AlphaGroupID equals e.AlphaGroupID into g
                           join c in ICDUnitOfWork.Codes.Find()
                               on a.CodeID equals c.CodeID into co
                           select new HomeSearchViewModel
                                      {
                                          Alphas = g,
                                          AlphaGroups = a,
                                          AlphaGroupCode = co.FirstOrDefault(),
                                          SearchTerm = searchTerm,
                                          AlphasCodes = null
                                      };

var codequery = from a in ICDUnitOfWork.Alphas.Find()
                join c in ICDUnitOfWork.Codes.Find()
                on a.CodeID equals c.CodeID into g
                select new HomeSearchViewModel
                       {
                          AlphasCodes = g
                       };

var allResults = queryNew.Concat(codequery);

This gives me an error stating:

The type 'ICD.ViewModels.HomeSearchViewModel' 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.

How can I join these results together?

Upvotes: 3

Views: 1438

Answers (5)

Hubeyb Özkul
Hubeyb Özkul

Reputation: 666

You must fill with null all other properties

var codequery = from a in ICDUnitOfWork.Alphas.Find()
                join c in ICDUnitOfWork.Codes.Find()
                on a.CodeID equals c.CodeID into g
                select new HomeSearchViewModel
                       {
                          Alphas = null,
                          AlphaGroups = null,
                          AlphaGroupCode = null,
                          SearchTerm = null,
                          AlphasCodes = g
                       };

var allResults = queryNew.Concat(codequery);

Upvotes: 0

user547794
user547794

Reputation: 14511

Well the solution was really dumb on my part. I added a navigation property to the table I was trying join and everything is working now.

whoops!

Upvotes: 1

user1793607
user1793607

Reputation: 531

If you don't mind doing it as two queries then calling AsEnumerable() will concatenate in local memory with no problems.

var result = queryNew.AsEnumerable().Concat(codequery); 

Here the AsEnumerable() will still defer execution of the queries (which is what your code seems to suggest), however if you want immediate execution do as Arthur suggests and call a cacheing function e.g. ToList() or ToArray()

Upvotes: 0

Arthur Nunes
Arthur Nunes

Reputation: 7048

You could try evaluating the queries before hand, calling something like "ToList()":

var allResults = queryNew.ToList().Concat(codequery.ToList());

Upvotes: 0

James
James

Reputation: 82096

Concat isn't really the right thing to do here, a simple for loop should be enough. From the looks of your query you could possibly use the AlphaGroupCode as your unique identifier for the mapping e.g.

var codequery = ...
                select new HomeSearchViewModel
                {
                    AlphaGroupCode = c.FirstOrDefault()
                    AlphasCodes = g
                };

foreach (var q in queryNew)
{
    q.AlphaCodes = codequery.Where(x => x.AlphaGroupCode == q.AlphaGroupCode)
                            .FirstOrDefault()
                            .AlphaCodes;
}

Upvotes: 0

Related Questions