实体框架中的行版本比较

编程入门 行业动态 更新时间:2024-10-28 12:30:20
本文介绍了实体框架中的行版本比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何使用实体框架比较rowversion字段?我有一个具有rowversion列的表,我想从行版本高于指定值的表中获取数据.

How should I compare rowversion fields using Entity Framework? I have one table which has a rowversion column, I want to get data from tables for which the row version is higher than specified value.

byte[] rowversion = ... some value; _context.Set<T>().Where(item => item.RowVersion > rowVersion);

此行不起作用,它会引发错误:

This line does not work, it throws the error:

运算符'>'不能应用于类型为'byte []'和'byte []'的操作数

有什么想法可以比较C#/Entity Framework中的rowversion字段吗?

Any idea how I can compare rowversion fields in C#/Entity Framework?

推荐答案

以下是我们为解决此问题所做的工作.使用这样的比较扩展名:

Here is what we did to solve this. Use a compare extension like this:

public static class EntityFrameworkHelper { public static int Compare(this byte[] b1, byte[] b2) { throw new Exception("This method can only be used in EF LINQ Context"); } }

然后您可以执行以下操作:

Then you can do this:

byte[] rowversion = .....somevalue; _context.Set<T>().Where(item => item.RowVersion.Compare(rowversion) > 0);

之所以没有C#实现的原因是,实际上从未调用过Compare扩展方法,并且EF LINQ将x.Compare(y) > 0简化为x > y.

The reason this works without a C# implementation is because the Compare extension method is never actually called, and EF LINQ simplifies x.Compare(y) > 0 down to x > y.

更多推荐

实体框架中的行版本比较

本文发布于:2023-11-25 08:11:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628978.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实体   框架   版本

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!