简体   繁体   English

在LINQ查询中将时间戳比较为byte []

[英]Comparing timestamp as byte[] inside LINQ Query

I've got a byte array that I need to compare to a SQL column, timestamp, which is also a byte array. 我有一个字节数组,需要将其与SQL列timestamp进行比较,这也是一个字节数组。 It's a timestamp that I need to get all records before or after from. 这是我需要获取之前或之后的所有记录的时间戳。

I'm using EF, and trying to get it to work in a LINQ query. 我正在使用EF,并尝试使其在LINQ查询中工作。

byte[] myArray = repo.GetTimeStampByDate(DateTime.Now);
rec.sometable.Where(o => o.timestamp <= myArray).ToList();

The easiest way is to convert both byte arrays to UInt64 values and then compare them. 最简单的方法是将两个字节数组都转换为UInt64值,然后进行比较。

Alternatively, you can create a custom method to compare timestamp arrays: 另外,您可以创建一个自定义方法来比较时间戳数组:

 // assuming both arrays are of equal lengths
 public static int Compare(byte[] ts1, byte[] ts2)
    {
        int i = 0;
        while (i < ts1.Length)
        {
            if (ts1[i] > ts2[i])
                return 1;
            else if (ts1[i] < ts2[i])
                return -1;
            ++i;
        }
        return 0;
    }

and then 接着

byte[] myArray = repo.GetTimeStampByDate(DateTime.Now);
rec.sometable.Where(o => Compare(o.timestamp, myArray) <= 0).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM