How does the Array operator work with DynamicQueryable?

dynamic-linq linq linq-to-sql


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);
var 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.

2/17/2011 11:05:39 AM

Accepted Answer

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",

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.

3/21/2011 9:02:02 PM

Related Questions

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow