Joining two collections which has different data type by using .net MongoDB driver

0

I am trying to join two MongoDB collections by using MongoDB Provider but Id field is "ObjectId" type and ProjectId is "String". I think because of that I got an error which I shared below. How can I fix that issue?

var projects = database.GetCollection<TrckTrace.Shared.Models.Common.Project>("project").AsQueryable();
var tasks = database.GetCollection<TrckTrace.Shared.Models.Common.Task>("task").AsQueryable();

var database = client.GetDatabase("trcktrace");

        var projects = database.GetCollection<TrckTrace.Shared.Models.Common.Project>("project").AsQueryable();
        var tasks = database.GetCollection<TrckTrace.Shared.Models.Common.ScrapingTask>("task").AsQueryable();

        var query = from project in projects
                    join task in tasks
                        on  project.Id.ToString() equals task.ProjectId
                        into joined
                    select new
                    {
                        Id = project.Id,
                        Name = project.Name,
                        Tasks = joined
                    };
        var res = query.ToList();

System.NotSupportedException HResult=0x80131515 Message=Unable to determine the serialization information for the outer key selector in the tree: aggregate([]).GroupJoin(aggregate([]), project => project.Id.ToString(), task => task.ProjectId, (project, matchedProjects) => new <>f__AnonymousType23(Id = project.Id, Name = project.Name, Tasks = matchedProjects)) Source=MongoDB.Driver
StackTrace: at MongoDB.Driver.Linq.Processors.Pipeline.MethodCallBinders.JoinBinder.Bind(PipelineExpression pipeline, PipelineBindingContext bindingContext, MethodCallExpression node, IEnumerable
1 arguments) at MongoDB.Driver.Linq.Processors.MethodInfoMethodCallBinder1.Bind(PipelineExpression pipeline, TBindingContext bindingContext, MethodCallExpression node, IEnumerable1 arguments) at MongoDB.Driver.Linq.Processors.PipelineBinderBase1.BindMethodCall(MethodCallExpression node) at MongoDB.Driver.Linq.Processors.Pipeline.PipelineBinder.Bind(Expression node, IBsonSerializerRegistry serializerRegistry) at MongoDB.Driver.Linq.MongoQueryProviderImpl1.Prepare(Expression expression) at MongoDB.Driver.Linq.MongoQueryProviderImpl1.Translate(Expression expression) at MongoDB.Driver.Linq.MongoQueryProviderImpl1.Execute(Expression expression) at MongoDB.Driver.Linq.MongoQueryableImpl2.GetEnumerator() at System.Collections.Generic.List1.AddEnumerable(IEnumerable1 enumerable) at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source) at DataMigration.DataMigration.d__5.MoveNext() in C:\Users\User01\Source\Repos\RabbitMQTest\DataMigration\DataMigration.cs:line 166 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at DataMigration.Program.d__0.MoveNext() in C:\Users\User01\Source\Repos\RabbitMQTest\DataMigration\Program.cs:line 23 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at DataMigration.Program.(String[] args)

.net
mongodb
.net-core
asked on Stack Overflow Nov 22, 2019 by leo • edited Nov 22, 2019 by Pavel Anikhouski

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0