将Linq-to-Sql查询的WHERE子句作为参数传递 [英] Passing a WHERE clause for a Linq-to-Sql query as a parameter

查看:224
本文介绍了将Linq-to-Sql查询的WHERE子句作为参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能会稍微突破Linq-to-Sql的界限,但鉴于到目前为止它的用途广泛,我想问一下.

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.

我有3个查询,它们选择的信息相同,只是在 where 子句中有所不同,现在我知道我可以传递一个委托,但这只允许我过滤已经返回的结果,但是想通过参数建立查询以确保效率.

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.

这是查询:

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.

谢谢

推荐答案

是的.

var times = DataContext.EventDateTimes;
if (cond)
    times = times.Where(time => time.StartDate <= ...);

from row in ... join time in times ...

这篇关于将Linq-to-Sql查询的WHERE子句作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆