LINQ 按日期间隔获取对象
我正在寻找一个 LINQ 查询,它将仅选择那些日期间隔不高于 20 秒的对象。例如:
AuthenticationEssay[] essays = new AuthenticationEssay[] {
new AuthenticationEssay() { Date = DateTime.Now.AddSeconds(20), Success = false },
new AuthenticationEssay() { Date = DateTime.Now.AddSeconds(24), Success = false },
new AuthenticationEssay() { Date = DateTime.Now.AddSeconds(29), Success = false },
new AuthenticationEssay() { Date = DateTime.Now.AddSeconds(38), Success = false },
new AuthenticationEssay() { Date = DateTime.Now.AddSeconds(125), Success = false },
new AuthenticationEssay() { Date = DateTime.Now.AddSeconds(347), Success = false },
new AuthenticationEssay() { Date = DateTime.Now.AddSeconds(400), Success = false },
new AuthenticationEssay() { Date = DateTime.Now.AddSeconds(422), Success = false },
new AuthenticationEssay() { Date = DateTime.Now.AddSeconds(446), Success = false },
new AuthenticationEssay() { Date = DateTime.Now.AddSeconds(467), Success = false }
};
我只想选择那些日期间隔不超过 20 秒的对象中第一次出现的对象与下一个对象。在这种情况下,查询应仅返回前 4 个对象。有什么想法吗? :(
更新
抱歉,我忘了提及我正在按降序对数组进行排序。所以是的,数组中的位置不应该对查询产生任何影响。
I'm looking for a LINQ query that will select only those objects whose date interval is not higher than 20 seconds. For example:
AuthenticationEssay[] essays = new AuthenticationEssay[] {
new AuthenticationEssay() { Date = DateTime.Now.AddSeconds(20), Success = false },
new AuthenticationEssay() { Date = DateTime.Now.AddSeconds(24), Success = false },
new AuthenticationEssay() { Date = DateTime.Now.AddSeconds(29), Success = false },
new AuthenticationEssay() { Date = DateTime.Now.AddSeconds(38), Success = false },
new AuthenticationEssay() { Date = DateTime.Now.AddSeconds(125), Success = false },
new AuthenticationEssay() { Date = DateTime.Now.AddSeconds(347), Success = false },
new AuthenticationEssay() { Date = DateTime.Now.AddSeconds(400), Success = false },
new AuthenticationEssay() { Date = DateTime.Now.AddSeconds(422), Success = false },
new AuthenticationEssay() { Date = DateTime.Now.AddSeconds(446), Success = false },
new AuthenticationEssay() { Date = DateTime.Now.AddSeconds(467), Success = false }
};
I want to select only the first occurence of those objects whose date interval is not longer than 20 seconds against the next object. In this case, the query should return only the first 4 objects. Any idea? :(
UPDATE
Sorry, I forgot to mention that I'm sorting the array by descending order. So yes, the position in the array shouldn't have any effect on the query.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这又如何呢?
编辑:显然,您必须调用
First()
才能仅获取序列的第一个元素。编辑2:我刚刚读到您正在对结果进行降序排序。在这种情况下,查询将略有不同:
What about this?
EDIT: Obviously you have to call
First()
in order to get only the first element of the sequence.EDIT 2: I have just read you are ordering your results descending. In this case the query will be slightly different:
它不太漂亮,但是你看吧...
必须是 LINQ 吗?我喜欢 LINQ,但我认为这样的东西会更具可读性......
It ain't pretty, but here you go...
Does it have to be LINQ? I love LINQ, but I think something like this would be more readable...
可能可以使用内置的 Linq 运算符来完成此操作,但在这种情况下,我认为编写特定的方法更容易。你可以这样做:
当然,通过使方法通用并传递谓词作为参数,可以使其更具可重用性,但由于这是一个非常具体的要求,我不确定它会非常有用...
It is probably possible to do it using built-in Linq operators, but in this case I think writing a specific method is easier. You could do something like that:
Of course it would be possible to make it more reusable, by making the method generic and passing a predicate as a parameter, but since it's a very specific requirement, I'm not sure it would be very useful...