相关(简化)课程:
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; }
}
我需要在给定主机列表的情况下填充所有服务的列表。因此,对于rsearch.hosts
中的每个主机,我需要rsearch.hostservices
中的所有服务。
这就是我现在所做的:
rsearch.hostservices = db.Services
.Where(j => rsearch.hosts.Select(c => c.HostID).Contains(j.HostID)).ToList();
但我总是得到以下错误
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.
答案 0 :(得分:2)
var ids = rsearch.hosts.Select(c => c.HostID).ToList();
rsearch.hostservices = db.Services
.Where(j => ids.Contains(j.HostID)).ToList();
答案 1 :(得分:1)
rsearch.hostservices = db.Services
.Where(j => rsearch.hosts.Select(c => c.HostID).ToList().Contains(j.HostID))
.ToList();