开发者

How does DynamicQueryable support the Array operator?

开发者 https://www.devze.com 2023-02-10 20:38 出处:网络
I have been using the DynamicQueryable Linq extensions featured in Scott Guthrie\'s blog post. The documentation has a table of supported operators.One of the primary operators is the following:

I have been using the DynamicQueryable Linq extensions featured in Scott Guthrie's blog post.

The documentation has a table of supported operators. One of the primary operators is the following:

x[…] Array or indexer access. Multi-dimensional arrays are not supported.

However, I cannot figure out how it can be used.

I didn't expect any of the following to work and in fact they don't.

var ctx = new MyDbContext();
var parameters = new Object[] { new int[] { 1, 2, 3 } };

var qry = ctx.Set<User>().Where<User>("it.Id in @0", parameters);
v开发者_C百科ar qry = ctx.Set<User>().Where<User>("it.Id.In(@0)", parameters);
var qry = ctx.Set<User>().Where<User>("it.Id = @0", parameters);
var qry = ctx.Set<User>().Where<User>("@0.Contains(it.Id)", parameters);

It is basically an In query, but I am not sure how to express it.


This is perhaps a misunderstanding. Meant is that it is possible to query for collection elements at a specific index position. For example:

public class Order
{
    public List<OrderDetail> OrderDetails { get; set; }
}

public class OrderDetail
{
    public string Description { get; set; }
}

Then you can query for all orders which have the Detail description "Bicycle" in the third OrderDetail by:

string parameter = "Bicycle";
var qry = ctx.Set<Order>().Where<Order>("it.OrderDetails[2].Description == @0",
    parameter);

I think for your purpose you need to build up an "OR" chain "it.Id == 1 or it.Id == 2 or it.Id == 3" (or build this query string dynamically in a loop) without parameters in the Where method.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号