This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Something like this does not work with Lightspeed (I believe it is not supported): int[] productList = new int[] { 1, 2, 3, 4 }; This should generate a query similar to this one SELECT * FROM Products WHERE id IN (1,2,3,4) Is there any workaround for this issue? I suspect that something might be achieved by fiddling with the Expression of the query, var myProducts = from p in db.Products select p Where for each pid in productList The idea is to iterate through the list and add an 'And' expression for each of the id-s.
|
|
|
You could use the LightSpeed core API, which supports the In operator directly: uow.Find<Product>(Entity.Attribute("Id").In(productList)); |
|
|
Unfortunately, I cannot go with this solution, because I am using LINQ queries due to some joins that we are performing. So, this approach wouldn't work for me. Can you combine 2 queries together? |
|
|
Well, anything you can do in a LINQ query you can also do through the core API (because LINQ queries eventually get turned into Query objects), but I understand your reluctance! Building a dynamic query in LINQ is a bit tricky and messy: some people have done some smart work that makes it easier (google around), but I'm just going to show you a quick and rudimentary solution to your immediate need. (Because of the test environment I had to hand, I've used a Comment type in my sample, rather than a Product. The translation should be trivial.) The basic idea is that we're going to create a set of Expression objects, each representing an equality test of a Comment.Id against a constant -- in C# terms, c.Id == someVal. We're going to compose them together as we go, using the OrElse expression -- in C# terms, the || operator. We're then going to turn that OrElse expression into a lambda with c as its parameter -- in C# terms, c => c.Id == v0 || c.Id == v1 || ... || c.Id = vn. int[] ids = new int[] { 81, 82, 83 };
|
|
|
Thanks for the response. This was exactly what I was looking for. However, I also found something that might be easier to use. Apparently there is an extension to LINQ which allows you to build dynamic LINQ queries (http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx). For instance, you can do something like this: Dim query As IQueryable(Of Complaint) = UnitOfWork.Complaints.Where("ReceivedDate < @0 and CountyId = @1 and Id = @2", Date.Now, 1, 2) This is neat and allows you to build dynamic queries at runtime, which what I was looking for. It might be useful for other people as well.
|
|