简体   繁体   English

vb.net至c#.net mongodb按日期排序

[英]vb.net to c#.net mongodb sort by date

I'm trying to get a small snippet of code from vb.net to c#.net using mongoDB driver working. 我正在尝试使用mongoDB驱动程序从vb.net到c#.net获取一小段代码。 It's almost working, but I can't seem to get the doc out of the mongoCursor in the same way I do from vb.net. 它几乎可以正常工作,但是我似乎无法像从vb.net一样将文档从mongoCursor中删除。

vb.net code: vb.net代码:

        Dim latest = updateCollection.FindAll.SetSortOrder(SortBy.Descending("date")).SetLimit(1)
        Dim latestDoc As BsonDocument = latest(0)

c#.net code: c#.net代码:

        var updateCollection = database.GetCollection<BsonDocument>("updateInfo");
        var sortby = SortBy.Descending("date");
        var latest = (updateCollection.FindAll().SetSortOrder(sortby).SetLimit(1));
        var latestDoc = latest.ToBsonDocument();

I've tried something like 我尝试过类似的东西

        var latestDoc = latest[0];

as well, but it hasn't worked. 一样,但是没有用。 What am I missing? 我想念什么?

Thank you! 谢谢!

You're getting a MongoCursor, not an item back from that that method, so you'll want to do something like this: 您将获得一个MongoCursor,而不是该方法返回的项目,因此您需要执行以下操作:

//add at the top of your code file
using System.Linq;


//...
//get the first result from the cursor like this:
var latestDoc = latest.FirstOrDefault();

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

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