Files
xDataService/Extensions/XMongoDbExtensions.cs
2024-01-25 04:41:17 +03:30

61 lines
1.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Core.Bindings;
using MongoDB.Driver.Core.Clusters;
using MongoDB.Driver.Core.Servers;
using MongoDB.Driver.Core.WireProtocol.Messages.Encoders;
using MongoDB.Driver.Linq;
using xDataService.Mongo;
namespace xDataService.Extensions {
public static class XMongoDbExtensions {
public static Guid ToGuid (this ObjectId source) {
//
var bytes = source
.ToByteArray ()
.Concat (new byte[] { 5, 5, 5, 5 })
.ToArray ();
//
var result = new Guid (bytes);
//
return result;
}
public static ObjectId ToObjectId (this Guid source) {
//
var bytes = source
.ToByteArray ()
.Take (12)
.ToArray ();
//
var result = new ObjectId (bytes);
//
return result;
}
public static IAsyncEnumerable<T> ToAsyncEnumerable<T> (this IAsyncCursorSource<T> asyncCursorSource) {
return new XAsyncEnumerableAdapter<T> (asyncCursorSource);
}
public static async IAsyncEnumerable<T> ToAsyncEnumerable<T> (this IAsyncCursor<T> source) {
while (await source.MoveNextAsync ()) {
foreach (var current in source.Current) {
yield return current;
}
}
}
public static XMongoAsyncCursor<T> ToAsyncCursor<T> (this IQueryable<T> source) {
return new XMongoAsyncCursor<T> (source);
}
}
}