如何在where子句追加到实体框架对象集

如何在where子句追加到实体框架对象集

本文介绍了如何在where子句追加到实体框架对象集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想追加一组条件where子句到一个对象集的结尾。但是,条款没有得到执行,而不是原来的运行查询,例如:

I would like to append a set of conditional where clauses onto the end of an ObjectSet. However, the clauses do not get executed and instead the original query is run, for example:

using (Entities context = new Entities()){
var q = context.AuditLogs;
q.Where(o => o.WebsiteId == 1);
}



在不执行条款,整个结果集返回
口可以转而使用的IQueryable为:

The where clause is not executed and the full result set is returnedI could instead use IQueryAble as in:

var q = context.AuditLogs.AsQueryable();
q = q.Where(o => o.WebsiteId == 1);



然而,这失去了我能够使用.INCLUDE跃跃欲试负荷我的相关实体的能力。

However this loses me the power of being able to use .Include to eager load my related entities.

推荐答案

没有,也不会。在执行查询之前任何时候,你仍然可以将它转换回的的ObjectQuery< T> 的并调用像包含的就可以了:

No, it won't. at any point before executing the query, you would still be able to cast it back to ObjectQuery<T> and invoke methods like Include on it:

var query = context.AuditLogs.AsQueryable();
query = query.Where(o => o.WebsiteId == 1);
var auditLog = ((ObjectQuery<AuditLog>)query).Include("yourNavPropertyName")
                                             .ToList();

如果你的目的是要逐步建立一个标准,那么其他的办法是杠杆的 EntitySQL 的用的的QueryBuilder 的方法:

If your intention is to build up a criteria incrementally, then the other option would be to leverage EntitySQL with QueryBuilder methods:

var query = context.AuditLogs.Where("it.WebsiteId = 1");
query = query.Where("...");
var auditLog = query.Include("yourNavPropertyName")
                    .ToList();

这篇关于如何在where子句追加到实体框架对象集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 08:15