EXISTS query in Dynamic Linq

Thread is closed for posting
2 posts, 0 answers
  1. Richard
    Richard avatar
    14 posts
    Member since:
    Oct 2010

    Posted 20 Dec 2010 Link to this post

    I have the following simple query in SQL:

    SELECT A.*
    FROM Products AS A
                  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

    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?
  2. PetarP
    PetarP avatar
    754 posts

    Posted 23 Dec 2010 Link to this post

    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.

    the Telerik team
    Accelerate your learning with industry's first Telerik OpenAccess ORM SDK. Download today.
Back to Top