繁体   English   中英

将Linq-to-Sql查询的WHERE子句作为参数传递

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

这可能会稍微限制Linq-to-Sql的界限,但是鉴于到目前为止它的用途如此广泛,我想我会问。

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

这是查询:

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)
};

我想通过参数应用的代码是:

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

这可能吗? 我认为不是,但我想先退房。

谢谢

是的。

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

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

您可以做的是传递一个允许过滤IQueryable的对象。 当您执行此操作时,可以编写如下代码作为您的服务层:

public Person[] GetAllPersons(IEntityFilter<Person> filter)
{
    IQueryable<Person> query = this.db.Persons;

    query = filter.Filter(query);

    return query.ToArray();
}

在调用层中,您可以定义一个过滤器,如下所示:

IEntityFilter<Person> filter =
    from person in EntityFilter<Person>.AsQueryable()
    where person.Name.StartsWith("a")
    where person.Id < 100
    select person;

// or (same result, but without LINQyness)
IEntityFilter<Person> filter = EntityFilter<Person>
    .Where(p => p.Name.StartsWith("a"))
    .Where(p => p.Id < 100);

// Call the BL with the filter.
var persons = BusinessLayer.GetAllPersons(filter);

您可以在此处找到此EntityFilter<T>的实现的源代码(大约40行代码),并在此处找到有关内容的博客文章。

请注意,您的查询比我在此处显示的示例要复杂一些,因此定义正确的过滤器可能需要更多的工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM