MongoDB的用C# - 动态生成的查询谓词 [英] MongoDB with C# - query with dynamically generated predicate

查看:347
本文介绍了MongoDB的用C# - 动态生成的查询谓词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有麻烦使用的是动态生成谓词查询MongoDB数据库。

I am having trouble querying a MongoDB database using a predicate which is dynamically generated.

该文件structue我试图查询的是:

The document structue i am trying to query on is:

{ 
    "_id" : 3121 , 
    "Active" : true , 
    "CategoryId" : 1 , 
    "Crci" : "IH" , 
    "CultureId" :  null  , 
    "DateUpdated" : { 
            "$date" : 1381916923120
    } , 
    "Description" : "National Careers Service: Actuary" , 
    "Keywords" : "" , 
    "MaxLevel" :  null  , 
    "MinLevel" :  null  , 
    "PhoneNumber" : "                    " , 
    "Priority" : 1 , 
    "Title" : "National Careers Service: Actuary" , 
    "WebUrl" : "https://nationalcareersservice.direct.gov.uk/advice/planning/jobprofiles/Pages/actuary.aspx" , 
    "CareerCultureExternalResources" : [ 
        { 
                "CareerId" : 5 , 
                "CultureId" : 1 , 
                "DisplayOrder" : 1 , 
                "ExternalResourceId" : 3121 , 
                "Vgs" :  null 
        }
    ] , 
    "SubjectExternalResources" : [ ] , 
    "LifestyleCategories" :  null
}

和我想利用在其中生成运行时例如谓词来查询:

And I am trying to query using at predicate which is generated a runtime E.g.:

Expression<Func<ExternalResourceView, bool>> predicate = predicateBuilder.True<ExternalResourceView>();
if (request.CultureId != null && request.CareerId != null) {
    predicate = predicate.And(er => er.CareerCultureExternalResources.Any(ccer => ccer.CareerId == request.CareerId.Value && ccer.CultureId == request.CultureId.Value));
} 
else if (request.SubjectAreaId != null && request.SubjectLevel != null && request.CultureId != null)
{
    predicate = predicate.And(er => er.SubjectExternalResources.Any(ser => ser.SubjectAreaId == request.CareerId && ser.CultureId == request.CultureId));
}
IEnumerable<ExternalResourceView> matchingExternalResources = _externalResourceLibrary.AsQueryable().Where(predicate)

当我尝试和执行查询,我得到错误:不支持的where子句:< InvocationExpression>

When I try and execute the query I get the error: "Unsupported where clause: <InvocationExpression>."

我无法找到,如果LINQ的到蒙戈能够从这样的表达或不生成查询(我认为它应该)。

I am unable to find if the Linq to Mongo is able to generate a query from such an expression or not (I think it should).

所以我只是试图做一些事情,在C#中蒙戈驱动程序不支持还是我没有产生谓语吧?我从 的另一个问题,我想要做的应该是可能的(至少在理论上)。

So am I just trying to do something that the C# Mongo driver doesn't support or am I not generating the predicate right? I have seen from another question that what I'm trying to do should be possible (in theory at least).

我使用的1.8.3版本

I am using the official 10gen driver ver 1.8.3

推荐答案

不知道它是MongoDB的一个解决方案,但你可以尝试使用 AsExpandable()从linqkit(由同一个人作为PredicateBuilder制造)的到来。

Not sure it's a solution for mongodb, but could you try to use AsExpandable() coming from linqkit (made by the same guy as PredicateBuilder).

LinqKit(约更多信息AsExpandable()和相关 PredicateBuilder ),可以发现这里

LinqKit (and more info about AsExpandable() and relation with PredicateBuilder) can be found here

这篇关于MongoDB的用C# - 动态生成的查询谓词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆