"paging thru result from mongodb in C#" Code Answer's
You're definitely familiar with the best coding language C# that developers use to develop their projects and they get all their queries like "paging thru result from mongodb in C#" answered properly. Developers are finding an appropriate answer about paging thru result from mongodb in C# related to the C# coding language. By visiting this online portal developers get answers concerning C# codes question like paging thru result from mongodb in C#. Enter your desired code related query in the search bar and get every piece of information about C# code related question on paging thru result from mongodb in C#.
paging thru result from mongodb in C#
public class Person
{
[BsonId]
[BsonRepresentation(BsonType.String)]
public string Id { get; set; }
public string FirstName { get; set; }
public string Surname { get; set; }
}
public class Peger
{
public int Count { get; set; }
public int Page { get; set; }
public int Size { get; set; }
public IEnumerable<Person> Items { get; set; }
}
class Program
{
static async Task Main(string[] args)
{
var client = new MongoClient();
var database = client.GetDatabase("pager_test");
var collection = database.GetCollection<Person>(nameof(Person));
int page = 1;
int pageSize = 5;
var results = await GetPagerResultAsync(page, pageSize, collection);
}
private static async Task<Peger> GetPagerResultAsync(int page, int pageSize, IMongoCollection<Person> collection)
{
// count facet, aggregation stage of count
var countFacet = AggregateFacet.Create("countFacet",
PipelineDefinition<Person, AggregateCountResult>.Create(new[]
{
PipelineStageDefinitionBuilder.Count<Person>()
}));
// data facet, we’ll use this to sort the data and do the skip and limiting of the results for the paging.
var dataFacet = AggregateFacet.Create("dataFacet",
PipelineDefinition<Person, Person>.Create(new[]
{
PipelineStageDefinitionBuilder.Sort(Builders<Person>.Sort.Ascending(x => x.Surname)),
PipelineStageDefinitionBuilder.Skip<Person>((page - 1) * pageSize),
PipelineStageDefinitionBuilder.Limit<Person>(pageSize),
}));
var filter = Builders<Person>.Filter.Empty;
var aggregation = await collection.Aggregate()
.Match(filter)
.Facet(countFacet, dataFacet)
.ToListAsync();
var count = aggregation.First()
.Facets.First(x => x.Name == "countFacet")
.Output<AggregateCountResult>()
?.FirstOrDefault()
?.Count ?? 0;
var data = aggregation.First()
.Facets.First(x => x.Name == "dataFacet")
.Output<Person>();
return new Pager
{
Count = (int)count / pageSize,
Size = pageSize,
Page = page,
Items = data
};
}
}
Source: stackoverflow.com
All those coders who are working on the C# based application and are stuck on paging thru result from mongodb in C# can get a collection of related answers to their query. Programmers need to enter their query on paging thru result from mongodb in C# related to C# code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about paging thru result from mongodb in C# for the programmers working on C# code while coding their module. Coders are also allowed to rectify already present answers of paging thru result from mongodb in C# while working on the C# language code. Developers can add up suggestions if they deem fit any other answer relating to "paging thru result from mongodb in C#". Visit this developer's friendly online web community, CodeProZone, and get your queries like paging thru result from mongodb in C# resolved professionally and stay updated to the latest C# updates.