2014-02-09 50 views
0

以下查詢任何人都可以請幫我轉換爲Lambda表達式如何將下面的sql轉換爲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

我認爲有些事情如下應該讓你開始。請注意,根據您的實際數據和關係,您可能需要對其進行優化:

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 => [email protected], eventType => eventType.eventtypeguid, (y, eventType) => new { y, eventType }) 
    .Join(serialcontainerevent, x => [email protected], 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() }); 
相關問題