1

I'm trying to display the results of a query, the difficulty is that each row in the table will include one Route object and a list of Visit objects. I want to save the query results in a list of RouteViewModels:

public class RouteViewModel
{
    public Route Route { get; set; }
    public List<Visit> Visits { get; set; }
}

I run the query and try to save the results:

var query =
            from r in db.Routes
            join v in visitList on r equals v.Route into visits
            select new { RouteObject = r, VisitList = visits };

int i = 0;
foreach (var prodGroup in query)
        {
            vm[i].Route = prodGroup.RouteObject; //vm is an list of RouteViewModels.
            j = 0;
            foreach(var prodItem in prodGroup.VisitList)
            {
                vm[i].Visits[j] = prodItem;
                j++;
            }
            i++;
        }

I wrote the foreach loop following answers to similar posts, but that line gives me the error:

Unable to create a constant value of type 'ApplicationDatabase.Visit'. Only primitive types or enumeration types are supported in this context

How do I access the Route and its list of Visits?

Toni
  • 73
  • 1
  • 2
  • 11
  • Which line is that error coming from? – Kendall Frey Apr 27 '15 at 13:21
  • After some Googling, it appears the problem may be that you're using `visitList`. ["You cannot join a set of data in the database with another set of data that is in memory."](http://stackoverflow.com/a/18944508/785745) – Kendall Frey Apr 27 '15 at 13:32
  • 1
    Try changing Visits type to IEnumerable. The, try enumerating over query.ToList() instead of query. This will actually force your query to execute, so you have all the data in memory. – qbik Apr 27 '15 at 14:34
  • Did the answer help you anyway.? – pjobs Apr 28 '15 at 18:11
  • Thanks, using @KendallFrey 's comment did the job - I had written a separate query to get `VisitList`, I removed that and included the query in the one above. – Toni Apr 29 '15 at 13:09

1 Answers1

0

I am not sure you are using EF, if you are then

If you have a Primarykey in the Route table then use that to compare objects. like

var query =
            from r in db.Routes
            join v in visitList on r.Id equals v.Route.Id into visits
            select new { RouteObject = r, VisitList = visits };
pjobs
  • 1,247
  • 12
  • 14