开发者

Is there a function similar to Math.Max for Entity Framework?

I have an entity framework query as follows;

From T In Db.MyTable
Where (T.Col1 - T.Col2) + T.Col3 - T.Col4 > 0 _
Select T

I now need to make sure that the bracketed part '(T.Col1 - T.Col2)' does not go below zero.

In .Net, I'd code it as follows (but obviously EF does not like Math.Max).

From T In Db.MyTable
Where Math.Max(T.Col1 - T开发者_JAVA技巧.Col2,0) + T.Col3 - T.Col4 > 0 _
Select T

Is there an easy way to do this? I am using EF 2.0 (not the latest, just released version).

Thanks in advance


Max isn't supported, but Abs is; will that do? Otherwise you'll have to use a ternary expression. In C#, I'd do:

from t in Db.MyTable
let m = t.Col1 >= t.Col2 ? t.Col1 - t.Col2 : 0
where m + t.Col3 - t.Col4 > 0

However, this will be inefficient at the DB level unless you have an expression index. So I'd suggest a computed column instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜