简体   繁体   English

MVC 3中的MySQL:不支持指定的方法

[英]MySQL in MVC 3: Specified method is not supported

in my MVC 3 project I'm usint the MySQL connector. 在我的MVC 3项目中,我使用的是MySQL连接器。 When I try to invoke that method, I get the Specified method is not supported error. 当我尝试调用该方法时,我得到了Specified method is not supported错误。 Why ? 为什么?

    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(); 
    }

edit 编辑

I see there's a problem with the x.Address.FirstOrDefault() != null 我看到x.Address.FirstOrDefault() != null

edit2 EDIT2

That code also works 该代码也有效

return (from x in db.Order
             from y in db.Address
             where (y.OrderID == x.OrderID)
        orderby x.Created descending
        select x).ToList();

With some ORM tools, not all of the LINQ methods are supported out of the box. 使用一些ORM工具,并非所有LINQ方法都支持开箱即用。 I know I have had that exact message using NHibernate in the past. 我知道过去使用NHibernate时我已经收到了确切的消息。 In order to achieve this you'll have to rewrite your logic to use more traditional methods. 为了实现这一目标,您必须重写逻辑以使用更传统的方法。 Try this: 尝试这个:

return (from x in db.Order 
            where x.Address.Count() > 0 
            orderby x.Created //it's a DateTime
            descending select x).ToList();

Or if for some reason you don't like the above, your other alternative is to first bring the collection in memory by resolving it, then perform your logic (although bear in mind this will be slower as it's bringing more data back from the database): 或者,如果由于某种原因你不喜欢上述内容,你的另一个选择是首先通过解析它将集合带入内存,然后执行你的逻辑(尽管记住这将会更慢,因为它会从数据库中带回更多数据):

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM