【发布时间】:2019-11-26 08:41:05
【问题描述】:
我正在开发使用MongoDb 作为数据库和.Net Core 3.0 作为框架的应用程序。为了从数据库中获取数据,我创建了一个DbContext 类并使用MongoDb 的Aggregation() 功能。我无法通过适当的投影。以下是DbContext.cs的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Microsoft.Extensions.Options;
using MongoDB.Driver;
namespace Test.DbContext
{
/// <summary>
/// Standard CRUD Operations with MongoDb
/// </summary>
public class MongoDbContext
{
#region Properties
private readonly IMongoClient _mongoDbClient = null;
private readonly IMongoDatabase _mongoDb = null;
#endregion
#region Constructor
public MongoDbContext(IOptions<MongoSetting> mongoConfigs)
{
_mongoDbClient = new MongoClient(mongoConfigs.Value.ConnectionString);
_mongoDb = _mongoDbClient.GetDatabase(mongoConfigs.Value.DatabaseName);
}
#endregion
#region Grouping
public IList<TProjection> GroupBy<TDocument, TGroupKey, TProjection>
(FilterDefinition<TDocument> filter,
Expression<Func<TDocument, TGroupKey>> selector,
Expression<Func<IGrouping<TGroupKey, TDocument>, TProjection>> projection){
return _mongoDb.GetCollection<TDocument>("collectionName").Aggregate().Match(filter).Group(selector, projection).ToList();
}
#endregion
}
}
要调用函数GroupBy(),我必须传递过滤器、选择器和投影,但我无法构建适当的表达式。以下是数据模型和调用函数:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Microsoft.Extensions.Options;
using MongoDB.Driver;
namespace Test
{
[BsonIgnoreExtraElements]
public class Employee
{
[BsonId]
[BsonElement("_id")]
[BsonRepresentation(BsonType.ObjectId)]
public ObjectId Id { get; set; }
[BsonElement("type")]
[JsonProperty("type")]
public string Type { get; set; }
[BsonElement("id")]
public string CustomerId { get; set; }
[BsonElement("name")]
public string CustomerName { get; set; }
}
}
我将客户存储库中的 dbContext 称为:
using System;
using System.Linq.Expressions;
using MongoDB.Driver;
namespace Test.Repositories
{
public class CustomerRepository : ICustomerRepository
{
#region Properties
private readonly IMongoDbContext _dbContext = null;
#endregion
#region Constructor
public CustomerRepository(IMongoDbContext dbContext)
{
_dbContext = dbContext;
}
#endregion
#region Methods
public EmployeeCollection GetSpecificData()
{
Expression<Func<Employee, dynamic>> filter = x => x.Employee.CustomerId == "11";
Expression<Func<Employee, dynamic>> selector = x => new { typeName = x.Employee.Type };
Expression<Func<IGrouping<dynamic, Employee>, dynamic>> projection = x => new
{
Key = x.Key,
count = x.Count(),
avgValue = x.Average(x => Convert.ToInt32(x.Employee.CustomerId))
};
var result = _dbContext.GroupBy<Employee, dynamic, dynamic>(filter, selector, projection);
// Giving exception
// "Value type of serializer is <>f__AnonymousType0`1[[System.String, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral,
//PublicKeyToken=7cec85d7bea7798e]] and does not match member type System.Object. (Parameter 'serializer')"
}
#endregion
}
}
例外:
"序列化器的值类型是f__AnonymousType0`1[[System.String, System.Private.CoreLib,版本=4.0.0.0,文化=中性, PublicKeyToken=7cec85d7bea7798e]] 与成员类型不匹配 系统对象。 (参数'序列化器')"
【问题讨论】:
标签: c# mongodb linq aggregation-framework