c# - 加入集合和列表在 C# Mongodb 强类型驱动程序中抛出 NotSupportedException

标签 c# .net mongodb mongodb-query mongodb-.net-driver

我正在使用官方的 C# MongoDb 强类型驱动程序版本 2.8.0 与 MongoDB 进行交互。

当我尝试将 Meals 类型的 mongodb 集合与 MealsRequest 类型的列表连接时,出现此异常:-

System.NotSupportedException: The joined collection cannot have any qualifiers."

这是我的代码:-

public class Meal
{
    [BsonId]
    [BsonRepresentation(representation: BsonType.ObjectId)]
    public string Id { get; set; }

    public string RestaurantId { get; set; }

    public string Category { get; set; }

    public string Name { get; set; }

    public decimal Price { get; set; }

    public int Calories { get; set; }

    public string Photo { get; set; }
}

public class MealRequest
{
    [BsonId]
    [BsonRepresentation(representation: BsonType.ObjectId)]
    public string Id { get; set; }

    [Required(ErrorMessage = "This field is required")]
    public string MealId { get; set; }

    [Required(ErrorMessage = "This field is required")]
    public int Count { get; set; }

    public decimal Price { get; set; }

    public decimal MealTotal { get; set; }

    public string Name { get; set; }
}

这是抛出异常的代码:-

var mealsRequests = await repository.Meals.AsQueryable()
    .Join(inner: mealsRequests, outerKeySelector: m => m.Id, innerKeySelector: mr => mr.MealId,
    resultSelector: (m, mr) => new MealRequest()
    {
        Id = mr.Id,
        MealId = m.Id,
        Count = mr.Count,
        Price = m.Price,
        Name = m.Name,
    }).ToListAsync();//Exception happens at this line 

return mealsRequests;

这是堆栈跟踪:-

System.NotSupportedException: The joined collection cannot have any qualifiers.
at MongoDB.Driver.Linq.Processors.Pipeline.MethodCallBinders.JoinBinder.Bind(PipelineExpression pipeline, PipelineBindingContext bindingContext, MethodCallExpression node, IEnumerable`1 arguments)
at MongoDB.Driver.Linq.Processors.MethodInfoMethodCallBinder`1.Bind(PipelineExpression pipeline, TBindingContext bindingContext, MethodCallExpression node, IEnumerable`1 arguments)
at MongoDB.Driver.Linq.Processors.PipelineBinderBase`1.BindMethodCall(MethodCallExpression node)
at MongoDB.Driver.Linq.Processors.Pipeline.PipelineBinder.Bind(Expression node, IBsonSerializerRegistry serializerRegistry)
at MongoDB.Driver.Linq.MongoQueryProviderImpl`1.Prepare(Expression expression)
at MongoDB.Driver.Linq.MongoQueryProviderImpl`1.Translate(Expression expression)
at MongoDB.Driver.Linq.MongoQueryProviderImpl`1.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken)
at MongoDB.Driver.Linq.MongoQueryableImpl`2.ToCursorAsync(CancellationToken cancellationToken)
at MongoDB.Driver.IAsyncCursorSourceExtensions.ToListAsync[TDocument](IAsyncCursorSource`1 source, CancellationToken cancellationToken)

这个异常的原因是什么?以及如何解决?

最佳答案

MongoDB.Entities 的关系和导航非常容易这是官方驱动程序的轻量级包装。即使以下示例是同步代码,它也具有异步功能。

using System.Linq;
using MongoDB.Entities;

namespace StackOverflow
{
    public class Meal : Entity
    {
        public string Name { get; set; }
        public Many<MealRequest> Requests { get; set; }

        public Meal() => this.InitOneToMany(() => Requests);
    }

    public class MealRequest : Entity
    {
        public One<Meal> Meal { get; set; }
        public string Name { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            new DB("retaurant-test");

            var meal = new Meal { Name = "pollo e funghi pasta" };
            meal.Save();

            var mealRequest = new MealRequest
            {
                Name = "pasta meal",
                Meal = meal.ToReference()
            };
            mealRequest.Save();

            meal.Requests.Add(mealRequest);

            var chickenMushroomPastaMealRequests = DB.Collection<MealRequest>()
                                                     .Where(mr => mr.Meal.ID == meal.ID)
                                                     .ToList();

            var resMealRequests = meal.Requests.Collection().ToList();

            var resMeal = resMealRequests.First().Meal.ToEntity();
        }
    }
}

如果必须使用官方驱动来完成,我会使用 linq 来完成,如下所示:

var mealrequests = (from m in repo.Meals.AsQueryable()
                    join mr in repo.MealRequests.AsQueryable() on m.ID equals mr.MealID into requests
                    from r in requests
                    select r).ToList();

关于c# - 加入集合和列表在 C# Mongodb 强类型驱动程序中抛出 NotSupportedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56086699/

相关文章:

c# - Windows 服务结束后,Quartz.net 作业似乎仍然存在

c# - EF Core 中已修改实体的自有类型属性不持久

c# - 以不同于 GET 的方式处理 POST

C#比较两个未知类型的对象(包括引用和值类型)

.net - 在文件签名 (.NET) 中使用魔数(Magic Number)验证文件类型的解决方案?

node.js - mongoose findOne 返回找到的第一个对象,即使条件不成立

c# - 如何在我的所有 Windows 上添加一个公共(public)控件?

用于 TCP 消息传递的 .NET 项目

gwt - 在 jetty 上部署 gwt web 应用程序

java - 如何使用 Java querybuilder 查询 MongoDB 的 ISODate 字段