This is a migrated thread and some comments may be shown as answers.

EXISTS query in Dynamic Linq

1 Answer 301 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Richard
Top achievements
Rank 1
Richard asked on 20 Dec 2010, 10:15 PM
I have the following simple query in SQL:

SELECT A.*
FROM Products AS A
WHERE EXISTS (SELECT B.Category_Id
              FROM Categories AS B
              WHERE B.Category_Type = 'Stationary'
              AND B.Category_Id = A.Category_Id)

I successfully converted this to a Linq query as follows:

from a in Products
where (from b in Categories
where b.Category_Type == 'Stationary'
select b.Category_Id).Contains(a.Category_Id)
select a

However, the 'Stationary' value should be dynamic. Therefore, I downloaded the Dynamic.vb library for inclusion in my project (via Includes System.Linq.Dynamic) so I can then dynamically build this query. I started with:

Dim query = from a in Products
query.where(...

and this is where I got stuck! Can anyone assist in making this a dynamic query where I can populate the Category_Type based on user input?

1 Answer, 1 is accepted

Sort by
0
PetarP
Telerik team
answered on 23 Dec 2010, 02:22 PM
Hi Richard,

If the stationary is a value why don't you pass it as a parameter like this:

Dim sampleParameter As String = "User Input"
from a in Products
where (from b in Categories
where b.Category_Type == sampleParameter
select b.Category_Id).Contains(a.Category_Id)
select a
You only need to put the user input into the sampleParameter parameter. The dynamic linq is used to dynamically switch between the things you query. So for example you can once query against the Category_Type and the next time query on Category_Id. As for your case if I have understood it correctly I believe you don't need to use any dynamic Linq.


Greetings,
Petar
the Telerik team
Accelerate your learning with industry's first Telerik OpenAccess ORM SDK. Download today.
Tags
LINQ (LINQ specific questions)
Asked by
Richard
Top achievements
Rank 1
Answers by
PetarP
Telerik team
Share this question
or