Ruby Sequel 等效于 LINQ to SQL 选择匿名对象

本文关键字:选择 SQL 对象 to LINQ Sequel Ruby | 更新日期: 2023-09-27 18:31:57

我有一些带有 LINQ 查询的 C# 代码,我正在尝试翻译成 Ruby,并使用 Sequel 作为我的数据库 ORM。linq 查询只执行一些联接,然后返回一个匿名对象,其中包含对联接实体的引用。我已经翻译了代码并正常运行,但它只是返回每个表中的所有列,我想将每组列包装在其自己的对象中,类似于在 C# 代码中完成的方式。

林克查询

from s in slots
join tu in _dbContext.table_usages on s.id equals tu.slot_id
join r in _dbContext.Reservations on tu.reservation_id equals r.ReservationID
join o in _dbContext.orders on r.OrderID equals o.OrderID
join c in _dbContext.Contacts on r.ContactID equals c.ContactID
where tu.reservation_id != null &&
      r.state != ReservationStates.Cancelled
select new { SlotId = s.id, Reservation = r, Order = o, Contact = c, TableUsage = tu };

红宝石代码:

select(:slots__id, :reservations.*, :orders.*, :restaurant_customers.*, :table_usages.*)
.filter(slots__restaurant_id: restaurant_id, slots__removed: false)
.filter(slots__slot_time: start_time..end_time)
.join(:table_usages, slot_id: :id)
.join(:reservations, id: :table_usages__reservation_id)
.join(:orders, id: :reservations__order_id)
.join(:restaurant_customers, id: :reservations__contact_id)
.filter('table_usages.reservation_id is not null')
.filter('reservations.state != ?', ReservationStates.cancelled)

无法通过文档找到完成此操作的方法,但我想我会看看是否有人以我还没有想到的方式做了类似的事情。

谢谢!

Ruby Sequel 等效于 LINQ to SQL 选择匿名对象

我假设你只是指最后两行:

.filter('table_usages.reservation_id is not null')
.filter('reservations.state != ?', ReservationStates.cancelled)

您可以通过以下方式处理:

.exclude(:table_usages__reservation_id=>nil)
.exclude(:reservations__states=>ReservationStates.cancelled)

排除用作反向滤波器。