Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Linq返回空数组的MongoDb查找方法_C#_Mongodb_Linq - Fatal编程技术网

C# 使用Linq返回空数组的MongoDb查找方法

C# 使用Linq返回空数组的MongoDb查找方法,c#,mongodb,linq,C#,Mongodb,Linq,我在MongoDb的同一个数据库中有两个集合,即“Notification”和“NotificationData”。我正在尝试加入两个集合,并从这两个集合中检索所需的信息 var query = notificationDataCollection.AsQueryable().Join( notificationCollection.AsQueryable(), p => p.Id, q =&

我在MongoDb的同一个数据库中有两个集合,即“Notification”和“NotificationData”。我正在尝试加入两个集合,并从这两个集合中检索所需的信息

 var query = notificationDataCollection.AsQueryable().Join(
                notificationCollection.AsQueryable(),
                p => p.Id,
                q => q.NotificationDataId,
                (p, q) => new
                {
                    ActorId = p.ActorId
                });
驱动程序已将结果转换为如下所示:


{aggregate([{ "$lookup" : { "from" : "Notification", "localField" : "_id", "foreignField" : "NotificationDataId", "as" : "q" } }, 
{ "$unwind" : "$q" }, 
{ "$project" : { "ActorId" : "$ActorId", "_id" : 0 } }])}
即使存在与给定条件匹配的记录,我也会得到一个空数组

例如:存储Mongo中的数据

通知

{"_id":{"$oid":"5e6b9564aece6902bcb0cd20"},
"NotificationDataId":"5e6b9563aece6902bcb0cd1f",
"NotifierId":"175ff977-c62a-45f4-9539-eb916c52db52",
"NotifierName":null,
"Status":{"$numberInt":"1"}}
通知数据

{"_id":{"$oid":"5e6b9563aece6902bcb0cd1f"},
"Entity":{"_t":"ProductQuoteRequest","UserId":null,"BuyerId":"eb30dfce-a2a3-49ef-8f86-eda48c636f1b",
"SellerId":"175ff977-c62a-45f4-9539-eb916c52db52",
"Subject":"string","Message":"Hey"},
"EntityType":"TraderZ.ProductQuoteRequest",
"ActorId":"eb30dfce-a2a3-49ef-8f86-eda48c636f1b",
"CreatedOn":{"$date":{"$numberLong":"1584108899661"}},
"Status":{"$numberInt":"1"}}

请注意字符串!=目标。看起来您的_id字段是ObjectId,但NotificationDataId字段是string。我确实尝试通过p.id.ToString()进行转换,但它引发了以下异常,无法确定树中外部键选择器的序列化信息:聚合([]).Join(聚合([]),p=>p.id.ToString(),q=>q.NotificationDataId,(p,q)=>新的f_u匿名类型0`1(Name=p.ActorId))您可能想尝试
(string)p.Id
,尽管我从未做过类似的事情。更好的解决方案肯定是修复数据库,使类型匹配。我试过这样做。到目前为止,它没有抛出异常,但结果仍然返回空值array@John我将NotificationDataId的类型从string转换为ObjectId,现在它可以工作了。