在我的MVC 3项目中,我使用的是MySQL连接器。当我尝试调用该方法时,出现Specified method is not supported
错误。为什么?
public IList<Order> GetOrders()
{
return (from x in db.Order
where (x.Address.FirstOrDefault() != null)
orderby x.Created //it's a DateTime
descending select x).ToList();
}
我发现x.Address.FirstOrDefault() != null
该代码也有效
return (from x in db.Order
from y in db.Address
where (y.OrderID == x.OrderID)
orderby x.Created descending
select x).ToList();
答案 0 :(得分:3)
使用一些ORM工具,并非所有LINQ方法都支持开箱即用。我知道过去使用NHibernate时我已经收到了确切的消息。为了实现这一目标,您必须重写逻辑以使用更多传统方法。试试这个:
return (from x in db.Order
where x.Address.Count() > 0
orderby x.Created //it's a DateTime
descending select x).ToList();
或者,如果由于某种原因你不喜欢上述内容,你的另一个选择是首先通过解析它来将内容带入内存,然后执行你的逻辑(尽管记住这将是更慢因为它从数据库中带回了更多数据):
return (from x in db.Order
orderby x.Created //it's a DateTime
descending select x)
.ToList() //resolve the query, now work with it in memory from here
.Where(x => x.Address.FirstOrDefault() != null)
.ToList();