Passing a WHERE clause for a Linq-to-Sql query as a parameter

Posted by Mantorok on Stack Overflow See other posts from Stack Overflow or by Mantorok
Published on 2010-04-26T09:33:05Z Indexed on 2010/04/26 9:43 UTC
Read the original article Hit count: 236

Filed under:
|

Hi all

This is probably pushing the boundaries of Linq-to-Sql a bit but given how versatile it has been so far I thought I'd ask.

I have 3 queries that are selecting identical information and only differ in the where clause, now I know I can pass a delegate in but this only allows me to filter the results already returned, but I want to build up the query via parameter to ensure efficiency.

Here is the query:

from row in DataContext.PublishedEvents
                    join link in DataContext.PublishedEvent_EventDateTimes on row.guid equals link.container
                    join time in DataContext.EventDateTimes on link.item equals time.guid
                    where row.ApprovalStatus == "Approved"
                          && row.EventType == "Event"
                          && time.StartDate <= DateTime.Now.Date.AddDays(1) &&
                          (!time.EndDate.HasValue || time.EndDate.Value >= DateTime.Now.Date.AddDays(1))
                   orderby time.StartDate
                   select new EventDetails
                       {
                          Title = row.EventName,
                          Description = TrimDescription(row.Description)
                       };

The code I want to apply via a parameter would be:

time.StartDate <= DateTime.Now.Date.AddDays(1) &&
                          (!time.EndDate.HasValue || time.EndDate.Value >= DateTime.Now.Date.AddDays(1))

Is this possible? I don't think it is but thought I'd check out first.

Thanks

© Stack Overflow or respective owner

Related posts about c#

Related posts about linq-to-sql