我一直在以代码优先的方式在实体框架上遇到以下问题。EF代码优先 - IQueryable在特定的小数属性上有乘法
我有一个小数的实体类,我有一个乘数小数参数。
我想构建一个查询(但不能调用它),它返回实体,但Bar
属性需要与我的参数相乘。
从编码侧:
public class Foo
{
public Guid Id { get; set; }
public Decimal Bar { get; set; }
}
// this simple stuff returns the entities after some filterings.
Context.Set<Foo>().Where(x => querying on many props).ToList();
这种方法类同什么我想要实现:
public IQueryable<Foo> GetFooQuery(.. Many properties used to the query .. , Decimal Multiplier)
{
var IQueryablePart = Context.Set<Foo>().Where(querying with the parameters);
/* ... and what to do here? ... */
/* IQueryablePart = IQueryablePart.Select(x => new {
Bar = Bar * Multiplier <-- this is okay
}); */
// but how to retrieve the other columns without listing them one by one, and how to return the data as IQueryable<Foo> ?
return IQueryablePart;
}
我想通过以下方式来使用此方法:
IQueryable<Foo> FullQuery = null;
for(some loop, may be 10 or 1000 iterations, it depends) {
var Part = GetFooQuery(/* .. query params ..*/, 2);
if(MyFullQuery == null)
FullQuery = Part;
else
FullQuery.Union(Part);
}
// and in the end, do the db call once:
var Result = FullQuery.ToList();
在SQL中,我会这样处理它:
SELECT
Id,
Bar * @MyValue as Bar,
# and all other columns
FROM
Foo
WHERE
(param queries 1) OR
(param queries 2) OR
---
(param queries N)
我的问题是:如何通过IQueryable
和EF来做到这一点?最重要的是,我只需要一次调用db。
我认为它可能是某些查询的建筑的东西,但我不熟悉它,但任何帮助将是非常赞赏。
如果两个'Bar'和'myvalue的:
最后,你可以通过执行单一的最终SQL查询,切换到LINQ到对象和转换
FooBar
到Foo
这样兑现的结果作为Foo
sequennce '是相同的表(即实体)的一部分,那么合乎逻辑的解决办法是在你的代码,而不是一个自动属性检索。 – silkfire不幸的是,他们不是。乘数由代码计算。 –
你从哪里得到@MyValue? – silkfire