如何通过多个 id 构建自定义 PLINQO 查询?

发布于 2024-10-25 20:33:33 字数 1237 浏览 2 评论 0原文

这是我的表结构

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));
}

我如何才能本质上合并这两个查询,让我传入一个 ListList?这个 LINQ/PLINQO 查询正在融化我的大脑。提前致谢!

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁人与我共长歌 2024-11-01 20:33:33

您需要编写如下扩展方法:

    public static IQueryable<Places> ByPlaceCats(this Table<Places> table, IEnumerable<int> catIds)
    {
        var db = (TestDataContext)table.Context;
        var places = (from placeCat in db.PlaceCats
                      join place in db.Places on placeCat.PlaceId equals place.PlaceId
                      where catIds.Contains(placeCat.CatId)
                      select place);
        return places;
    }

请注意,PlaceCats 表可以制作为 多对多关系,通过向适当的表添加两个外键来实现。进行此更改后, PLINQO 将自动生成正确的代码并创建一个两个表之间的链接跳过中间表。因此,您可以通过访问 Places 实体上的属性来获取与当前 Places 实体关联的 PlaceCategories 集合。

如果您有任何疑问,请记得联系我们,并务必查看位于此处这里是 PLINQO 论坛

谢谢
-Blake Niemyjski(CodeSmith 支持)

You would need to write a extension method like this:

    public static IQueryable<Places> ByPlaceCats(this Table<Places> table, IEnumerable<int> catIds)
    {
        var db = (TestDataContext)table.Context;
        var places = (from placeCat in db.PlaceCats
                      join place in db.Places on placeCat.PlaceId equals place.PlaceId
                      where catIds.Contains(placeCat.CatId)
                      select place);
        return places;
    }

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文