如何分配 IQueryable的属性值?
我正在使用 Entity Framework 4.1 Code First。在我的实体中,我具有三个日期/时间属性:
public class MyEntity
{
[Key]
public Id { get; set; }
public DateTime FromDate { get; set; }
public DateTime ToDate { get; set; }
[NotMapped]
public DateTime? QueryDate { get; set; }
// and some other fields, of course
}
在数据库中,我始终填充“从/到”日期。我使用一个简单的 where 子句对它们进行查询。但在结果集中,我想包含我查询的日期。我需要坚持这一点才能使其他一些业务逻辑发挥作用。
我正在研究一种扩展方法来执行此操作,但遇到了问题:
public static IQueryable<T> WhereDateInRange<T>(this IQueryable<T> queryable, DateTime queryDate) where T : MyEntity
{
// this part works fine
var newQueryable = queryable.Where(e => e.FromDate <= queryDate &&
e.ToDate >= queryDate);
// in theory, this is what I want to do
newQueryable = newQueryable.Select(e =>
{
e.QueryDate = queryDate;
return e;
});
return newQueryable;
}
这不起作用。如果我使用 IEnumerable,它就可以工作,但我想将其保留为 IQueryable,以便一切都在数据库端运行,并且此扩展方法仍然可以在另一个查询的任何部分中使用。当它是 IQueryable 时,我收到以下编译错误:
带有语句体的 lambda 表达式无法转换为表达式树
如果这是 SQL,我只会执行以下操作:
SELECT *, @QueryDate as QueryDate
FROM MyEntities
WHERE @QueryDate BETWEEN FromDate AND ToDate
所以问题是,如何转换我已经必须包含此额外属性分配的表达式树?我研究过 IQueryable.Expression 和 IQueryable.Provider.CreateQuery - 那里有一个解决方案。也许可以将赋值表达式附加到现有的表达式树中?我对表达式树方法不够熟悉,无法解决这个问题。有什么想法吗?
示例用法
澄清一下,目标是能够执行如下操作:
var entity = dataContext.Set<MyEntity>()
.WhereDateInRange(DateTime.Now)
.FirstOrDefault();
并将 DateTime.Now 保留到结果行的 QueryDate 中,而不会从数据库查询返回多行。 (使用 IEnumerable 解决方案,在 FirstOrDefault 选择我们想要的行之前会返回多行。)
另一个想法
我可以继续将 QueryDate 像真实字段一样映射,并将其 DatabaseGenerateOption 设置为 Computed。但随后我需要某种方法将“@QueryDate as QueryDate”注入到 EF 的 select 语句创建的 SQL 中。由于它是经过计算的,因此 EF 不会在更新或插入期间尝试提供值。那么我该如何将自定义 SQL 注入到 select 语句中呢?
I'm using Entity Framework 4.1 Code First. In my entity, I have three date/time properties:
public class MyEntity
{
[Key]
public Id { get; set; }
public DateTime FromDate { get; set; }
public DateTime ToDate { get; set; }
[NotMapped]
public DateTime? QueryDate { get; set; }
// and some other fields, of course
}
In the database, I always have the From/To dates populated. I query against them using a simple where clause. But in the result set, I want to include the date I queried for. I need to persist this for some other business logic to work.
I'm working on an extension method to do this, but I'm running into problems:
public static IQueryable<T> WhereDateInRange<T>(this IQueryable<T> queryable, DateTime queryDate) where T : MyEntity
{
// this part works fine
var newQueryable = queryable.Where(e => e.FromDate <= queryDate &&
e.ToDate >= queryDate);
// in theory, this is what I want to do
newQueryable = newQueryable.Select(e =>
{
e.QueryDate = queryDate;
return e;
});
return newQueryable;
}
This doesn't work. It works if I use an IEnumerable, but I want to keep it as IQueryable so everything runs on the database side, and this extention method can still be used in any part of another query. When it's IQueryable, I get a compile error of the following:
A lambda expression with a statement body cannot be converted to an expression tree
If this was SQL, I would just do something like this:
SELECT *, @QueryDate as QueryDate
FROM MyEntities
WHERE @QueryDate BETWEEN FromDate AND ToDate
So the question is, how can I transform the expression tree I already have to include this extra property assignment? I have looked into IQueryable.Expression and IQueryable.Provider.CreateQuery - there's a solution in there somewhere. Maybe an assignment expression can be appended to the existing expression tree? I'm not familiar enough with the expression tree methods to figure this out. Any ideas?
Example Usage
To clarify, the goal is to be able to perform something like this:
var entity = dataContext.Set<MyEntity>()
.WhereDateInRange(DateTime.Now)
.FirstOrDefault();
And have the DateTime.Now persisited into the QueryDate of the resulting row, WITHOUT having more than one row returned from the database query. (With the IEnumerable solution, multiple rows are returned before FirstOrDefault picks the row we want.)
Another Idea
I could go ahead and map QueryDate like a real field, and set its DatabaseGeneratedOption to Computed. But then I would need some way to inject the "@QueryDate as QueryDate" into the SQL created by EF's select statements. Since it's computed, EF won't try to provide values during update or insert. So how could I go about injecting custom SQL into the select statements?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不,我不认为有解决办法。确实,您可以修改表达式树,但您将得到与 linq 查询完全相同的异常,因为该查询实际上是您将在表达式树中构建的内容。问题不在于表达式树,而在于映射。 EF 无法将
QueryData
映射到结果。此外,您正在尝试进行投影。无法对映射实体进行投影,并且无法从该方法返回匿名类型。您当然可以执行您提到的选择,但只是无法将其映射到您的实体。您必须为此创建一个新类型:
No, I don't think there is a solution. It is true that you can modify expression tree but you will get exactly the same exception as you got with your linq query because that query actually is what you will build in expression tree. The problem is not in expression tree but in the mapping. EF can't map
QueryData
to the result. Moreover you are trying to do projection. Projection can't be done to mapped entity and anonymous type can't be returned from the method.You can off course do the select you mentioned but simply you can't map it to your entity. You must create a new type for that:
Automapper 有 Queryable Extensions,我认为它可以解决您的需求。
您可以使用 ProjectTo 在运行时计算属性。
Ef Core 2 将值设置为运行时忽略的属性
http://docs.automapper.org/en/stable/Queryable- Extensions.html
示例配置:
用法:
Automapper has Queryable Extensions, i think it can resolve your needs.
You can use ProjectTo to calculate property on runtime.
Ef Core 2 set value to ignored property on runtime
http://docs.automapper.org/en/stable/Queryable-Extensions.html
Example configuration:
Usage:
感谢您提供的所有宝贵反馈。听起来答案是“不——你不能那样做”。
所以 - 我想出了一个解决方法。这对于我的实现来说非常具体,但它确实有效。
神奇的是,我使用线程静态字段来缓存查询日期,以便稍后在同一线程中可用。事实上,我在 QueryDate 的 getter 中取回它是特定于我的需求的。
显然,这不是原始问题的 EF 或 LINQ 解决方案,但它通过将其从该世界中删除来实现相同的效果。
Thank you for all of the valuable feedback. It sounds like the answer is "no - you can't do it that way".
So - I figured out a workaround. This is very specific to my implementation, but it does the trick.
The magic is that I'm using a thread static field to cache the query date so it's available later in the same thread. The fact that I get it back in the QueryDate's getter is specific to my needs.
Obviously this isn't an EF or LINQ solution to the original question, but it does accomplish the same effect by removing it from that world.
拉迪斯拉夫是绝对正确的。但由于您显然希望回答问题的第二部分,因此以下是如何使用“分配”。但这不适用于 EF。
更新 1
这是经过一些调整后的相同代码。我去掉了上面代码中 EF 阻塞的“Block”表达式,以绝对清晰地证明 EF 不支持“Assign”表达式。请注意,Assign 原则上适用于通用表达式树,EF 提供程序不支持Assign。
Ladislav is absolutely right. But since you obviously want the second part of your question to be answered, here is how you can use Assign. This won't work with EF, though.
Update 1
Here is the same code after some tweaking. I got rid of the "Block" expression, that EF choked on in the code above, to demonstrate with absolute clarity that it's "Assign" expression that EF does not support. Note that Assign works in principle with generic Expression trees, it is EF provider that does not support Assign.