Reputation: 631
Relevant (simplified) classes:
public class Host
{
public int HostID { get; set; }
public String HostName { get; set; }
public String Description { get; set; }
public bool Active { get; set; }
}
public class Service
{
public int ServiceID { get; set; }
public String Description { get; set; }
public bool Active { get; set; }
public int HostID { get; set; }
public virtual Host Host { get; set; }
}
I need to populate a list of all services given a list of hosts. So for every host in rsearch.hosts
, I need all the services in rsearch.hostservices
.
This is what I do now:
rsearch.hostservices = db.Services
.Where(j => rsearch.hosts.Select(c => c.HostID).Contains(j.HostID)).ToList();
But I always get the following error
System.NotSupportedException: Unable to create a constant value of type 'xxx.Models.Host'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.
Upvotes: 0
Views: 55
Reputation: 6366
rsearch.hostservices = db.Services
.Where(j => rsearch.hosts.Select(c => c.HostID).ToList().Contains(j.HostID))
.ToList();
Upvotes: 1
Reputation: 63095
var ids = rsearch.hosts.Select(c => c.HostID).ToList();
rsearch.hostservices = db.Services
.Where(j => ids.Contains(j.HostID)).ToList();
Upvotes: 2