动态Linq谓词会抛出“不支持的过滤器". C#MongoDB驱动程序错误 [英] Dynamic Linq Predicate throws "Unsupported Filter" error with C# MongoDB Driver

查看:203
本文介绍了动态Linq谓词会抛出“不支持的过滤器". C#MongoDB驱动程序错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用Linq将表达式的动态列表传递给MongoDB C#驱动程序查询...例如,该方法适用于针对ORM的常规Linq查询,但应用于时会导致错误一个MongoDB查询...(仅供参考:我也在使用LinqKit的PredicateBuilder)

I have been trying to pass in a dynamic list of Expressions to a MongoDB C# Driver query using Linq ... This method works for me with regular Linq queries against an ORM, for example, but results in an error when applied to a MongoDB query ... (FYI: I am also using LinqKit's PredicateBuilder)

//
// I create a List of Expressions which I can then add individual predicates to on an 
// "as-needed" basis.
    var filters = new List<Expression<Func<Session, Boolean>>>();

//
// If the Region DropDownList returns a value then add an expression to match it.
// (the WebFormsService is a home built service for extracting data from the various 
// WebForms Server Controls... in case you're wondering how it fits in)
    if (!String.IsNullOrEmpty(WebFormsService.GetControlValueAsString(this.ddlRegion)))
    {
        String region = WebFormsService.GetControlValueAsString(this.ddlRegion).ToLower();
        filters.Add(e => e.Region.ToLower() == region);
    }

//
// If the StartDate has been specified then add an expression to match it.
    if (this.StartDate.HasValue)
    {
        Int64 startTicks = this.StartDate.Value.Ticks;
        filters.Add(e => e.StartTimestampTicks >= startTicks);
    }

//
// If the EndDate has been specified then add an expression to match it.
    if (this.EndDate.HasValue)
    {
        Int64 endTicks = this.EndDate.Value.Ticks;
        filters.Add(e => e.StartTimestampTicks <= endTicks);
    }

//
// Pass the Expression list to the method that executes the query
    var data = SessionMsgsDbSvc.GetSessionMsgs(filters);

GetSessionMsgs()方法是在数据服务类中定义的...

The GetSessionMsgs() method is defined in a Data services class ...

public class SessionMsgsDbSvc
{

    public static List<LocationOwnerSessions> GetSessionMsgs(List<Expression<Func<Session, Boolean>>> values)
    {
        //
        // Using the LinqKit PredicateBuilder I simply add the provided expressions 
        // into a single "AND" expression ...
            var predicate = PredicateBuilder.True<Session>();
            foreach (var value in values)
            {
                predicate = predicate.And(value);
            }

        //
        // ... and apply it as I would to any Linq query, in the Where clause.
        // Additionally, using the Select clause I project the results into a 
        // pre-defined data transfer object (DTO) and only the DISTINCT DTOs are returned
            var query = ApplCoreMsgDbCtx.Sessions.AsQueryable()
                .Where(predicate)
                .Select(e => new LocationOwnerSessions 
                    { 
                        AssetNumber = e.AssetNumber, 
                        Owner = e.LocationOwner, 
                        Region = e.Region 
                    })
                .Distinct();

            var data = query.ToList();

            return data;
    }
}

使用LinqKit PredicateBuilder,我只需将提供的表达式添加到单个"AND"表达式中,然后将其像在任何Where()子句中一样应用于任何Linq查询.另外,使用Select()子句将结果投影到预定义的数据传输对象(DTO)中,并且仅返回DISTINCT DTO.

Using the LinqKit PredicateBuilder I simply add the provided expressions into a single "AND" expression ... and apply it as I would to any Linq query, in the Where() clause. Additionally, using the Select() clause I project the results into a pre-defined data transfer object (DTO) and only the DISTINCT DTOs are returned.

当我对我的Telerik ORM上下文实体集合进行攻击时,此技术通常有效……但是当我对Mongo Document Collection运行该操作时,出现以下错误...

This technique typically works when I an going against my Telerik ORM Context Entity collections ... but when I run this against the Mongo Document Collection I get the following error ...

不支持的过滤器:调用(e =>(e.Region.ToLower()=="central"), {document})

Unsupported filter: Invoke(e => (e.Region.ToLower() == "central"), {document})

我还不清楚在封面下有什么事情发生.在 MongoDB文档我发现以下注释...

There is certainly something going on beneath the covers that I am unclear on. In the C# Driver for MongoDB documentation I found the following NOTE ...

投影标量时,驱动程序会将标量包装到一个 具有生成的字段名称的文档,因为MongoDB要求 聚合管道的输出是文档"

"When projecting scalars, the driver will wrap the scalar into a document with a generated field name because MongoDB requires that output from an aggregation pipeline be documents"

但是说实话,我不确定这意味着什么,或者它是否与这个问题有关.错误中出现"{document}"表明它可能是相关的.

But honestly I am not sure what that neccessarily means or if it's related to this problem or not. The appearence of "{document}" in the error suggests that it might be relevant though.

任何其他想法或见解将不胜感激.现在已经停留了2天的大部分时间了……

Any additional thoughts or insight would be greatly appreciated though. Been stuck on this for the better part of 2 days now ...

我确实找到了 这篇文章 ,但到目前为止,我不确定所接受的解决方案与我所做的有何不同.

I did find this post but so far am not sure how the accepted solution is much different than what I have done.

推荐答案

.Where(predicate.Compile())

这将使用IQueryable<T>接口上的Func<T, bool>方法触发,MongoDB驱动程序可以很好地与之配合使用.

That will trigger using the Func<T, bool> method on the IQueryable<T> interface which the MongoDB driver will work just fine with.

这篇关于动态Linq谓词会抛出“不支持的过滤器". C#MongoDB驱动程序错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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