【发布时间】:2020-02-12 11:59:13
【问题描述】:
有没有办法在最后一次购买日期之前订购所有客户?
例如
ctx.Customers.OrderByDescending(e => e.Purchases.LastOrDefault().DateTime);
应该是这样的,但是,这不起作用。它抛出异常
LINQ to Entities does not recognize the method 'Purchase
LastOrDefault[Purchase]
(System.Collections.Generic.IEnumerable`1[Purchase])'
method, and this method cannot be translated into a store expression
编辑:
public class Customer
{
public Customer()
{
Purchases = new List<Purchase>();
}
public int Id { get; set; }
public string Name { get; set; }
[JsonIgnore]
public virtual IList<Purchase> Purchases { get; set; }
}
public class Purchase
{
public int Id { get; set; }
public int IdCustomer { get; set; }
public DateTime DateTime { get; set; }
public virtual Customer Customer { get; set; }
}
在上下文中我确实有类似的东西
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.HasRequired(s => s.Customer)
.WithMany(p => p.Purchases)
.HasForeignKey(s => s.IdCustomer);
}
【问题讨论】:
-
编辑:对不起,我打错了。即使 LastOrDefault 是方法,错误仍然存在。
-
你能显示一些代码吗?持有
Purchases属性的类和持有DateTime属性的类? -
什么是
e.Purchases? -
@Çöđěxěŕ 它是购买的 IList
-
@CorentinPane 我可以,我展示的代码应该可以工作?