Using an existing IQueryable to create a new dynamic IQueryable

Posted by dnoxs on Stack Overflow See other posts from Stack Overflow or by dnoxs
Published on 2010-05-27T21:00:32Z Indexed on 2010/06/01 10:53 UTC
Read the original article Hit count: 562

I have a query as follows:

var query = from x in context.Employees
    where (x.Salary > 0 && x.DeptId == 5) || x.DeptId == 2
    order by x.Surname
    select x;

The above is the original query and returns let's say 1000 employee entities.

I would now like to use the first query to deconstruct it and recreate a new query that would look like this:

var query = from x in context.Employees
    where ((x.Salary > 0 && x.DeptId == 5) || x.DeptId == 2) && (x,i) i % 10 == 0
    order by x.Surname
    select x.Surname;

This query would return 100 surnames.

The syntax is probably incorrect, but what I need to do is attach an additional where clause and modify the select to a single field.

I've been looking into the ExpressionVisitor but I'm not entirely sure how to create a new query based on an existing query.

Any guidance would be appreciated. Thanks you.

© Stack Overflow or respective owner

Related posts about entity-framework

Related posts about linq-to-entities