2

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

LINQ 查询

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)

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

谢谢!

4

1 回答 1

0

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

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

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

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

exclude 用作逆过滤器。

于 2012-04-21T17:51:07.307 回答