2012-04-20 60 views
2

我有一些LINQ查詢的C#代碼我試圖翻譯成Ruby,並使用Sequel作爲我的DB ORM。 linq查詢只是執行一些連接,然後返回一個包含對連接實體的引用的匿名對象。我已經將代碼轉換並正確運行,但它只是返回每個表中的所有列,我想將每組列都包裝在它自己的對象中,就像它在C#代碼中的做法一樣。Ruby續集相當於LINQ to SQL選擇匿名對象

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) 

我無法找到通過文檔實現這個的一種方式,但我想我會看看是否已經有人做過類似的東西一種我還沒有想到的方式。

謝謝!

回答

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) 

排除逆濾波器工作。

+0

我不是,我更指的是第一線,上雖然排除良好的通話。我已經將它們更改爲帶有{{}的過濾器來反轉它,但這更好,不知道爲什麼我沒有想到 – Jimmy 2012-04-26 19:36:42