2014-02-09 40 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() }); 
相关问题