如何将以下sql转换为lambda表达式?

时间:2014-02-09 13:25:05

标签: c# lambda

对于以下查询,任何人都可以帮我转换为lambda表达式

SELECT p.partno,
   sp.property,
   count(s.serialNo) AS PropertyCount
FROM events e
JOIN Products p ON p.productguid = e.productguid
JOIN eventtypes et ON et.eventtypeguid = e.eventtypeguid
JOIN serialcontainerevent sce ON sce.EventGUID = e.EventGUID
JOIN serials s ON s.serialguid = sce.serialguid
JOIN statuses st ON st.statusguid = s.statusguid
LEFT OUTER JOIN serialproperties sp ON sp.serialguid = s.serialguid
WHERE p.partno = '21101'
  AND st.code = 'NOTRECEIVED'
  AND e.Field1Value = '21101'
  AND e.Field2Value = '21101'
  AND e.Field3Value = '21101'
  AND e.Field4Value = '21101'
  AND e.Field5Value = '21101'
  AND sp.property = 'Delivery Date' --group by p.partno,s.serialno
GROUP BY p.partno,
         sp.property

1 个答案:

答案 0 :(得分:1)

我认为以下内容应该让你开始。请注意,您可能需要根据实际数据和关系对其进行优化:

events
    .Where(@event => @event.Field1Value == "21101" && @event.Field2Value == "21101" && @event.Field3Value == "21101" && @event.Field4Value == "21101" && @event.Field5Value == "21101")
    .Join(products.Where(product => product.partno == "21101"), @event => @event.productguid, product => product.productguid, (@event, product) => new { @event, product })
    .Join(eventtypes, y => y.@event.eventtypeguid, eventType => eventType.eventtypeguid, (y, eventType) => new { y, eventType })
    .Join(serialcontainerevent, x => x.y.@event.EventGUID, serialContainerEvent => serialContainerEvent.EventGUID, (x, serialContainerEvent) => new { x, serialContainerEvent })
    .Join(serials, w => w.serialContainerEvent.serialguid, serial => serial.serialguid, (w, serial) => new { w, serial })
    .Join(statuses.Where(status => status.code == "NOTRECEIVED"), v => v.serial.statusguid, status => status.statusguid, (v, status) => new { v, status })
    .GroupJoin(serialproperties.Where(serialProperty => serialProperty.property == "Delivery Date"), u => u.v.serial.serialguid, serialProperty => serialProperty.serialguid, (u, serialProperties) => new { u, serialProperties })
    .SelectMany(t => t.serialProperties.Select(s => new { key = new { t.u.v.w.x.y.product.partno, s.property }, t }))
    .GroupBy(r => r.key)
    .Select(z => new { z.Key.partno, z.Key.property, PropertyCount = z.Where(q => q.t.u.v.serial.serialNo != null).Count() });