簡體   English   中英

相當於LINQ to SQL的Ruby Sequel選擇匿名對象

[英]Ruby Sequel Equivalent to LINQ to SQL select anonymous objects

我有一些帶有LINQ查詢的C#代碼,我試圖將其轉換為Ruby,並使用Sequel作為我的數據庫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 };

Ruby代碼:

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)

我找不到通過文檔完成此操作的方法,但我想我會看看是否有人以我從未想到的方式做了類似的事情。

謝謝!

我假設您只是在指最后兩行:

.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用作逆濾波器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM