我想制作一个功能来获取产品不同的客户名称
I want to Make a function that get product distinct customers names
我想编写一个函数来获取产品不同的客户名称
我试过这个功能。我认为问题出在 where 子句中。请告诉我是否有任何其他方法可以做到这一点
public string[] GetProductDistinctCustomers(int productId)
{
return _context.Orders
.Include(o => o.OrderDetails)
.Where(o => o.OrderDetails.Exists(od => od.ProductId == productId))
.Select(o => o.User.UserName)
.Distinct()
.ToArray();
}
但我得到 InvalidOperationException
。
类 :
public class Order
{
public int OrderId { get; set; }
public List<OrderDetail> OrderDetails { get; set; }
public string UserId { get; set; }
public AppUser User { get; set; }
}
public class OrderDetail
{
public int OrderDetailId { get; set; }
public int Amount { get; set; }
public int ProductId { get; set; }
public Product Product { get; set; }
public int OrderId { get; set; }
public Order Order { get; set; }
}
public class AppUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
感谢您给我的任何提示
异常:
InvalidOperationException: The LINQ expression
'DbSet()
.Where(o => MaterializeCollectionNavigation(
Navigation: Order.OrderDetails,
subquery: DbSet()
.Where(o0 => EF.Property<Nullable>(o, "OrderId") != null && object.Equals(
objA: (object)EF.Property<Nullable>(o, "OrderId"),
objB: (object)EF.Property<Nullable>(o0, "OrderId"))).Exists(od => od.ProductId == __productId_0))'
could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
Entity Framework 不知道方法 Exists
。将其替换为 Any
:
public string[] GetProductDistinctCustomers(int productId)
{
return _context.Orders
.Include(o => o.OrderDetails)
.Where(o => o.OrderDetails.Any(od => od.ProductId == productId))
.Select(o => o.User.UserName)
.Distinct()
.ToArray();
}
此外,对于这种情况,您可以删除 Include
。
我想编写一个函数来获取产品不同的客户名称 我试过这个功能。我认为问题出在 where 子句中。请告诉我是否有任何其他方法可以做到这一点
public string[] GetProductDistinctCustomers(int productId)
{
return _context.Orders
.Include(o => o.OrderDetails)
.Where(o => o.OrderDetails.Exists(od => od.ProductId == productId))
.Select(o => o.User.UserName)
.Distinct()
.ToArray();
}
但我得到 InvalidOperationException
。
类 :
public class Order
{
public int OrderId { get; set; }
public List<OrderDetail> OrderDetails { get; set; }
public string UserId { get; set; }
public AppUser User { get; set; }
}
public class OrderDetail
{
public int OrderDetailId { get; set; }
public int Amount { get; set; }
public int ProductId { get; set; }
public Product Product { get; set; }
public int OrderId { get; set; }
public Order Order { get; set; }
}
public class AppUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
感谢您给我的任何提示
异常:
InvalidOperationException: The LINQ expression
'DbSet() .Where(o => MaterializeCollectionNavigation( Navigation: Order.OrderDetails, subquery: DbSet() .Where(o0 => EF.Property<Nullable>(o, "OrderId") != null && object.Equals( objA: (object)EF.Property<Nullable>(o, "OrderId"), objB: (object)EF.Property<Nullable>(o0, "OrderId"))).Exists(od => od.ProductId == __productId_0))'
could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
Entity Framework 不知道方法 Exists
。将其替换为 Any
:
public string[] GetProductDistinctCustomers(int productId)
{
return _context.Orders
.Include(o => o.OrderDetails)
.Where(o => o.OrderDetails.Any(od => od.ProductId == productId))
.Select(o => o.User.UserName)
.Distinct()
.ToArray();
}
此外,对于这种情况,您可以删除 Include
。