2012-04-20 69 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