我正在尝试检索 rowversion 大于某个值的 SQL 记录。在 SQL 中,这是微不足道的(WHERE RowVersion > x),但在动态 Linq 查询中并非如此。
在我的 EF 模型中,我已将时间戳注释应用于相应的列。
[Timestamp]
public byte[] RowVersion { get; set; }
并使用静态比较扩展进行 byte[] 比较(来源:(https://stackoverflow.com/a/42502969/3424480)
static class MiscExtension
{
public static int Compare(this byte[] b1, byte[] b2)
{
if (b1 == null && b2 == null)
return 0;
else if (b1 == null)
return -1;
else if (b2 == null)
return 1;
return ((IStructuralComparable)b1).CompareTo(b2, Comparer<byte>.Default);
}
}
我尝试了以下样式的动态 Linq 表达式,但没有成功(maxRV 是一个小字节序字节 [])。
.Where("RowVersion.Compare(@0) > 0", maxRV);
这会引发“不存在适用的聚合方法 'Compare'”,经过一些研究,我认为这是因为 'Compare' 不是有效的可枚举方法。任何关于我可能出错/替代方法的指示都表示赞赏。