我在我的例子中使用了实体框架。我想过滤子实体但它在运行时打破程序时出错:“实体或复杂类型'CodeFirstNamespace.Customer'不能在LINQ to Entities查询中构造。”谁能帮我?感谢。
public class Customer
{
public int CustomerID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
//public virtual ICollection<Address> Addresses
public List<User> Users { get; set; }
public List<Address> Addresses { get; set; }
public List<Order> Orders { get; set; }
public List<CheckProduct> CheckProducts { get; set; }
}
public Customer GetCustomerCheckProduct(string email, Byte checkType)
{
IQueryable<Customer> customers = context.Set<Customer>().Include("CheckProducts").Where(c => c.Email == email);
if (checkType != 0)
{
var cus = customers.Select(customer => new Customer { CheckProducts = customer.CheckProducts.Where(s => s.CheckType == checkType).ToList() }).SingleOrDefault();
return cus;
}
return null;
}
答案 0 :(得分:1)
假设您要查找具有给定类型CheckProducts的第一个(如果有)客户,为什么不简单地使用它?
var cus = customers.FirstOrDefault(c => c.CheckProducts.Any(cp => cp.Checktype == checkType));
答案 1 :(得分:0)
要过滤集合导航属性,您可以执行以下操作:
var customer = <get customer entity>;
var filteredCheckProducts = context.Entry( customer )
.Collection( c => c.CheckProducts )
.Query()
.Where( cp => cp.CheckType == checkType )
.ToArray();