[英]Sub-Query or Join with embedded/nested document using C# LINQ with MongoDB
我正在尝试执行类似于波纹管示例的操作,但得到的异常为 - System.ArgumentException: 类型“System.Collections.Generic.IEnumerable 1 ' cannot be used for parameter of type 'System.Linq.IQueryable
表达式1 ' cannot be used for parameter of type 'System.Linq.IQueryable
方法的1 ' cannot be used for parameter of type 'System.Linq.IQueryable
1” 1 ' cannot be used for parameter of type 'System.Linq.IQueryable
。 这是我的代码和相关类。 我该如何解决这个问题,除了尝试做之外,还有什么方法可以解决。
var channels = _channelService.Collection;
var tracks = _trackService.Collection;
var query = from b in tracks.AsQueryable()
select b;
var data = (from q in channels.AsQueryable()
from p in q.Episodes
//from x in trackcoll.AsQueryable()
select new
{
p,
Tracks = query.Where(w => p.Tracks.Contains(w.Id))
}).ToList();
// Related classes
public class ContentBase : IAuditable
{
public string Id { get; set ; }
public string CreatedBy { get ; set ; }
public string CreatedOn { get ; set ; }
public string UpdatedBy { get ; set ; }
public string UpdatedOn { get; set; }
}
public class Channel: ContentBase
{
public List<Episode> Episodes { get; set; }
}
public class Episode: ContentBase
{
// List of track Id
public List<string> Tracks { get; set; }
}
public class Track: ContentBase
{
public string TrackUrl { get; set; }
public string Duration { get; set; }
public string Size { get; set; }
public string ContentType { get; set;
}
MongoDB 对连接的 LINQ 支持仅限于此处所述的相等连接。 您的表达式无法转换为聚合框架的$lookup,因为.Contains()
没有等效的语法。
因此,您必须运行更接近聚合框架语法的操作。 一个例子是流畅的聚合接口,它允许您运行与聚合框架的运算符同名的扩展方法。 尝试:
var q = _channels.Aggregate()
.Unwind(x => x.Episodes)
.Lookup(
foreignCollectionName:"tracks",
localField:"Episodes.Tracks",
foreignField:"_id",
@as:"Tracks");
var result = q.ToList();
上面的代码将返回一个List<BsonDocument>
mickl 的回答会让你使用官方驱动程序,但如果你不喜欢处理 bsondocuments 并且想要某种程度的类型安全,你可以简单地使用mongodb.entities库(我是作者):
public class EpisodeWithTracks
{
public Track[] Tracks { get; set; }
}
var pipeline = new Template<Channel, EpisodeWithTracks>(@"
[
{
$unwind: '$<Episodes>'
},
{
$lookup: {
from: '<track_collection>',
localField: '<Episodes.Tracks>',
foreignField: '_id',
as: '<Tracks>'
}
}
]")
.Path(c => c.Episodes)
.Tag("track_collection", collectionName)
.Path(c => c.Episodes[0].Tracks)
.PathOfResult(ewt => ewt.Tracks);
var result = DB.Aggregate(pipeline)
.ToList();
这是解释它如何工作的维基页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.