c# - 动态 Linq 谓词在 C# MongoDB 驱动程序中引发 "Unsupported Filter"错误

标签 c# mongodb linq mongo-c-driver mongodb-.net-driver

我一直在尝试使用 Linq 将表达式的动态列表传递给 MongoDB C# 驱动程序查询...例如,此方法适用于针对 ORM 的常规 Linq 查询,但在应用于时会导致错误一个 MongoDB 查询 ...(仅供引用:我也在使用 LinqKit 的 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() 方法在数据服务类中定义...

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。

这种技术通常在我反对我的 Telerik ORM 上下文实体集合时有效......但是当我针对 Mongo 文档集合运行它时,我得到以下错误......

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

肯定有一些我不清楚的事情在幕后发生。在 MongoDB documentation 的 C# 驱动程序中我发现了以下注意...

"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}”表明它可能是相关的。

任何额外的想法或见解将不胜感激。现在已经坚持了 2 天的大部分时间......

我确实找到了 this post 但到目前为止我不确定接受的解决方案与我所做的有何不同。

最佳答案

我将在 4 年后回来重新审视这个问题,因为虽然我最初的假设确实有效,但它以错误的方式工作,即从 Mongo 中撤回所有记录,然后在内存中过滤它们并混合它正在处理的事情对数据库进行同步调用,这始终不是一个好主意。

The magic happens in LinqKit's expand extension method

这将调用表达式树扁平化为 Mongo 驱动程序可以理解并据此采取行动的东西。

.Where(predicate.Expand())

关于c# - 动态 Linq 谓词在 C# MongoDB 驱动程序中引发 "Unsupported Filter"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37056386/

相关文章:

c# - 如何使用 WCF 服务和 javascript 表单发布上传 .doc 文件

c# - TCP 连接阶段失败(从 JAVA 客户端调用 Web 服务到 C# 服务器)

javascript - 如何构造日期范围查询?

IGrouping 的 C# 内联初始化

c# - 如何转换 ASP.NET session 以取回我的列表?

c# - 如何使用 WMI 删除 DNS 区域

c# - 我无法思考如何在 C# 中使用正则表达式或如何解决我的问题

python - 如何使用 pymongo 构建变量查询

node.js - 模式中的 Mongoose geojson, "Can' t 提取地理 key “错误

c# - 使用 LINQ to SQL 对搜索结果进行分页