如何通过多个 id 构建自定义 PLINQO 查询?
这是我的表结构
Places
- PlaceId PK
- Name
- ...
PlaceCategories
- CatId PK
- Name
- ...
PlaceCats
- PlaceId PK
- CatId PK
这是我提取地点的查询基于类别 id(表连接)
public static IQueryable<Places> ByPlaceCat(this Table<Places> table, Expression<Func<PlaceCats, bool>> predicate) {
var db = (DataContext)table.Context;
var innerBizBase = db.PlaceCats.Where(predicate);
return db.Places.Join(innerBizBase, a => a.PlaceId, ab => ab.PlaceId, (a, ab) => a);
}
我这样使用它:
places = Db.Places.ByPlaceCat(a => a.CatId == 5);
但我希望能够基于类别 id 的 List
进行拉取。查看生成的 PLINQO 代码,通过多个 PlaceId(但不使用连接表)提取的查询如下所示:
public static IQueryable<Places> ByPlaceId(this IQueryable<Places> queryable, IEnumerable<long> values)
{
return queryable.Where(p => values.Contains(p.PlaceId));
}
我如何才能本质上合并这两个查询,让我传入一个 List
List
Here's my table structure
Places
- PlaceId PK
- Name
- ...
PlaceCategories
- CatId PK
- Name
- ...
PlaceCats
- PlaceId PK
- CatId PK
Here's my query that pulls Places based on category id (table join)
public static IQueryable<Places> ByPlaceCat(this Table<Places> table, Expression<Func<PlaceCats, bool>> predicate) {
var db = (DataContext)table.Context;
var innerBizBase = db.PlaceCats.Where(predicate);
return db.Places.Join(innerBizBase, a => a.PlaceId, ab => ab.PlaceId, (a, ab) => a);
}
I use it like this:
places = Db.Places.ByPlaceCat(a => a.CatId == 5);
But I want to be able to pull based on a List<int>
of category id's. Looking through the generated PLINQO code, a query that pulls by multiple PlaceId's (but not using a joined table) looks like this:
public static IQueryable<Places> ByPlaceId(this IQueryable<Places> queryable, IEnumerable<long> values)
{
return queryable.Where(p => values.Contains(p.PlaceId));
}
How could I essentially merge those two queries, to let me pass in a List<int>
of CatId's to query by? This LINQ/PLINQO query is melting my brain. Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要编写如下扩展方法:
请注意,PlaceCats 表可以制作为 多对多关系,通过向适当的表添加两个外键来实现。进行此更改后, PLINQO 将自动生成正确的代码并创建一个两个表之间的链接跳过中间表。因此,您可以通过访问 Places 实体上的属性来获取与当前 Places 实体关联的 PlaceCategories 集合。
如果您有任何疑问,请记得联系我们,并务必查看位于此处 和 这里是 PLINQO 论坛。
谢谢
-Blake Niemyjski(CodeSmith 支持)
You would need to write a extension method like this:
Please note that the PlaceCats table could be made into a ManyToMany relationship by adding two foreign keys to the proper tables. Once this change has been made than PLINQO will automatically generate the correct code and will create a link between the two tables skipping the intermediary table. So you could get a collection of PlaceCategories associated to the current Places entity by accessing a property on the Places entity.
Please remember to contact us if you have any questions and be sure to check out the community forums located here and PLINQO forums here.
Thanks
-Blake Niemyjski (CodeSmith Support)