Subsonic 3 ActiveRecord nested select for NotIn bug?

那年仲夏 提交于 2019-12-06 04:24:40

Just came across this exact same issue in the latest release, so apparently it hasn't been fixed yet. I tried switching the order of the conditions (putting the NotIn condition first) and that did the trick. Here's what the new code looks like, which produced parameters @0 and @1 instead of @0 and @0:

var q = new SubSonic.Query.Select().Top("1")
    .From("Order")
    .Where(OrderTable.CustomerId).NotIn(
        new Subsonic.Query.Select("CustomerId")
            .From("Customer")
            .Where("TypeId")
            .IsNotEqualTo(typeId)
    )
    .And("ShopId")
    .IsEqualTo(shopId)
    .OrderDesc("NewId()");

I'm not sure about SubSonic 3 but in SubSonic 2 if you would run this code the inner query would be executed first and the second query would have the CategoryIds allready defined as a parameter in the query.
Maybe this is a bug and you should post it on github.

Anyway you could make your query work for the moment and behave like a SubSonic 2 Subquery with this little change:

var q = new SubSonic.Query.Select().Top("1")
    .From("Order")
    .Where("ShopId")
    .IsEqualTo(shopId)
    .And(OrderTable.CustomerId).NotIn(
        new Subsonic.Query.Select("CustomerId")
            .From("Customer")
            .Where("TypeId")
            .IsNotEqualTo(typeId)
            .ExecuteTypedList<int>()
    )
    .OrderDesc("NewId()");

NotIn should take a IEnumerable as a parameter but q will contain the whole list of CustomerIds as a parameter before the outer part is executed.

Not a real solution but a quick fix for the moment (if it doesn't affect performance to much).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!