如何使用Entity Framework比较rowversion
字段?我有一个表有一个rowversion
列,我想从表中获取行版本高于指定值的数据。
//code
byte[] rowversion = .....somevalue;
_context.Set<T>().Where(item => item.RowVersion > rowVersion);
此行不起作用,它会抛出错误:
不能应用于'byte []'和'byte []'
类型的操作数
知道如何比较c#/ EF中的rowversion
字段吗?
答案 0 :(得分:3)
以下是我们为解决这个问题所做的工作:
使用比较扩展名,如下所示:
ln -s /full/path1/project/public /full/path/to/public_html
然后你可以做
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");
}
}
没有C#实现的原因是因为实际上从未调用compare扩展方法,而且EF LINQ将byte[] rowversion = .....somevalue;
_context.Set<T>().Where(item => item.RowVersion.Compare(rowversion) > 0);
简化为x.compare(y) > 0
答案 1 :(得分:1)
你确定Kosher使用的是RowVersion吗?翻身时会发生什么?
我认为您需要先将其转换为string或int。例如
Encoding.Unicode.GetString(ba)
您也可以从EF拨打SP。 : - )
答案 2 :(得分:0)
使用EntitySQL。
// Drop down to ObjectContext and query against ObjectSet:
var objectContext = (dbContext as IObjectContextAdapter).ObjectContext;
// Create param for EntitySQL
var param = new ObjectParameter("rowVersion", rowVersion);
// Create IQueryable<T>
var query = objectContext.CreateObjectSet<T>.Where("it.RowVersion > @rowVersion",param);