0

我有以下在本地工作的代码:

public async Task GetConnections(string enterpriseId, IEnumerable<PermittedCommunicationType> permittedCommunication)
{
    IQueryable<ConnectionGetDto> query;
    query = await Task.Run(() =>
                from conn in _connectionCollection.AsQueryable()
                where (conn.EnterpriseId == enterpriseId)
                join consumer in _consumerCollection.AsQueryable() on conn.ConsumerId equals consumer.Id into joined
                select new ConnectionGetDto
                {
                    Id = conn.Id,
                    AdditionalData = conn.AdditionalData,
                    FirstName = joined.First().Profile.FirstName,
                }
                into p
                select p);

    query = ApplyEnterpriseConnectionPermissionFilter(query, permittedCommunication);

    var totalRecords = query.Count(); // Error when using Cosmos DB
}

private IQueryable<ConnectionGetDto> ApplyFilter(IQueryable<ConnectionGetDto> query, IEnumerable<CommunicationType> communicationType)
{
    if (permittedCommunication != null)
    {
        foreach (var p in permittedCommunication)
        {
            switch (p)
            {
                case PermittedCommunicationType.Call:
                    query = query.Where(c => c.PermittedCommunication.Call);
                    break;
                case PermittedCommunicationType.Email:
                    query = query.Where(c => c.PermittedCommunication.Email);
                    break;
                case PermittedCommunicationType.Im:
                    query = query.Where(c => c.PermittedCommunication.Im);
                    break;
                case PermittedCommunicationType.Letter:
                    query = query.Where(c => c.PermittedCommunication.Letter);
                    break;
                case PermittedCommunicationType.Sms:
                    query = query.Where(c => c.PermittedCommunication.Sms);
                    break;
            }
        }
    }

    return query;
}

但是 - 当我将它部署到 Cosmos DB 时,在这一行:

var totalRecords = query.Count();

我收到以下错误:

{
  "_t": "OKMongoResponse",
  "ok": 0,
  "code": 118,
  "errmsg": "$match is currently only supported when it is the first and only stage of the aggregation pipeline. Please restructure your query to combine multiple $match stages into a single $match stage.",
  "$err": "$match is currently only supported when it is the first and only stage of the aggregation pipeline. Please restructure your query to combine multiple $match stages into a single $match stage."
}

这似乎是由ApplyFilter对查询的方法修改引起的。

有谁知道我可以如何重组我的查询,以便在构建查询的主要部分后仍然可以添加可选参数?

4

0 回答 0