在MongoDB上使用通用存储库的表达式转换错误

时间:2019-06-12 09:32:42

标签: c# mongodb generics expression func

我在建立通用的mongodb存储库时遇到了一些麻烦 (我已将集合设为GenericRepositoryEntry)

#include "class.hpp"

extern "C" {
#include "obj.h"
}


A::A(int i)
{
  x = allocate_Ctype();
  x->i = i;
}

A::~A()
{
  deallocate_Ctype(x);
}

这是回购集合类

 public static void SaveRepositoryEntry<TDataObject>(
        TDataObject msg,
        Func<GenericRepositoryEntry<TDataObject>, bool> finder01,
        Expression<Func<GenericRepositoryEntry<TDataObject>, bool>> finder02,
        Func<GenericRepositoryEntry<DataValue>, bool> finder03,
        Expression<Func<GenericRepositoryEntry<DataValue>, bool>> finder04)
        where TDataObject : DataValue
    { 
            using (var ctx = new MongoContext())
            {
                IMongoQueryable<GenericRepositoryEntry<DataValue>> iQuery = ctx.DataValues.AsQueryable();

                var items01 = iQuery.Where(x => finder01(x)).ToList(); //compile error
                var items02 = iQuery.Where(finder02).ToList(); //compile error
                var items03 = iQuery.Where(x => finder03(x)).ToList();
                var items04 = iQuery.Where(finder04).ToList();
            }
    }

items01:

 public class GenericRepositoryEntry<T>
 {
    [BsonId(IdGenerator = typeof(CombGuidGenerator))]
    public Guid Id { get; set; }

    public T DataObject { get; set; }
    public DateTime LastEditDate { get; set; }
    public bool ActualFlag { get; set; }
 }

items02:

cannot convert from 'GlobalElectricGateway.Core.Model.MongoEntry.GenericRepositoryEntry<GlobalElectricGateway.Core.Model.Ocpp.DataValue>' to 'GlobalElectricGateway.Core.Model.MongoEntry.GenericRepositoryEntry<TDataObject>'

items03和items04

还可以...因为我不使用泛型类型:(

我在items01 / items02上收到编译错误 似乎正在忽略- cannot convert from 'System.Linq.Expressions.Expression<System.Func<GlobalElectricGateway.Core.Model.MongoEntry.GenericRepositoryEntry<TDataObject>, bool>>' to 'System.Linq.Expressions.Expression<System.Func<GlobalElectricGateway.Core.Model.MongoEntry.GenericRepositoryEntry<GlobalElectricGateway.Core.Model.Ocpp.DataValue>, int, bool>>'

还尝试将SaveRepositoryEntry方法签名更改为

where TDataObject : DataValue

并更改repoEntryClass

where TDataObject : IMongoEntryDataItem

0 个答案:

没有答案