1

I'm using a LINQ to Entities, and I have a couple of queries for which I want to be able to specify the Select clause at runtime.

I figured I'd have to do it by building an Expression and adding it to the IQueryable, but I'm not sure how to do this. Can anybody give me a hint?

1
  • can you give a specific example of what you want to achieve? There might be another way of doing things. Commented Jun 7, 2011 at 13:19

2 Answers 2

1

I am not sure you could do what you want with expressions. The select clause specifies the type of the object in the IQueryable collection, that has to be defined at compile time. There is something called Dynamic Linq that can do what you want.

Sign up to request clarification or add additional context in comments.

1 Comment

I am aware of dynamic Linq, but that's not really what I'm looking for. You make a good point about the generic type of the IQueryable, though, which makes me think the scenario I have in mind might not be possible. Thanks!
0

Something like this:

IQueryable<cerberus_Ticket> matches = db.cerberus_Tickets;


 if (this.AgentIdField.Text.Trim().Length > 0)
 {
     matches = matches.Where(a => a.AgentId == criteria.AgentId);
 }

  if (this.TicketIdField.Text.Trim().Length > 0)
 {
     matches = matches.Where(a => a.TicketId.Contains(criteria.TicketId));
 } 

var output = matches.ToList();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.