5

我一直在尝试使用 Linq 将表达式的动态列表传递给 MongoDB C# Driver 查询...例如,此方法适用于针对 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 文档集合运行它时,我得到以下错误......

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

在我不清楚的情况下,肯定有一些事情在幕后发生。在 C# Driver for MongoDB 文档中,我发现了以下注意...

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

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

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

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

4

1 回答 1

7

4年后我会回来重新审视这个问题,因为虽然我最初的假设确实有效,但它的工作方式是错误的,即它从 Mongo 拉回所有记录,然后在内存中过滤它们,并且为了复合问题,它正在进行同步调用进入数据库,这总是一个坏主意。

魔法发生在 LinqKit 的扩展扩展方法中

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

.Where(predicate.Expand())
于 2016-07-19T00:34:09.543 回答