开发者

Dynamically construct Select clause of a Linq query

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?


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.


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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜