林完全新手在LINQ和Lamda表達式。 我有來自多個數據庫表中檢索信息有點複雜的SQL語句:複雜SQL到LINQ在C#
SELECT A.OrderID, A.FORMJDENO, A.Title, B.Descr, C.Code, C.Descr, D.ModificationDate from OrderForm A
LEFT JOIN OrderPriority B ON A.OrderPriorityID = B.OrderPriorityID
LEFT JOIN StockClass C ON A.StockClassID = C.StockClassID
LEFT JOIN AuditTrailLog D ON A.OrderID = D.ObjectID
WHERE D.ColumnInfoID= 487 AND D.OldValue='1' AND D.NewValue='2' AND A.FormStatus=2 AND A.FormType=3 AND B.OrderPriorityID=1000001 AND C.StockClassID=1000002
AND A.DeptID IN
(SELECT DeptID FROM Department WHERE InstID = 1000006)
AND DATEDIFF(m,D.ModificationDate, A.VendorDeliveryDate) >= 3
我已經有LINQ差不多完成了,使用.Contains()
法取代WHERE...IN
SQL條款,但我需要一些幫助,使德joins
限制結果基於屬於其他表的值,並在LINQ中使用DATEDIFF
等效值。 這就是我已經得到的並且工作正常,但顯然它不限制上述SQL語句的結果。我嘗試了幾種方式,但沒有成功。我需要相當於LINQ
UPDATE:
最後我能得到它的工作。這是檢索與SQL語句相同記錄的最終LINQ。非常感謝@Gert阿諾德:
var valid = dba.OrderForm
.Where(q => q.FormType == 3
&& q.FormStatus == 2
&& q.OrderPriority.OrderPriorityID == orderpriorityID
&& q.StockClass.StockClassID == stockclassID
&& dba.AuditTrailLog.Where(log => q.OrderID==log.ObjectID)
.Any(log => log.ColumnInfoID == 487
&& log.OldValue == "1"
&& log.NewValue == "2"
&& EntityFunctions.DiffMonths(log.ModificationDate,
q.VendorDeliveryDate) >= period)
&& departments.Contains(q.DeptID));
LINQ到實體?關聯OrderForm - AuditTrailLog的多樣性是什麼? – 2013-05-09 12:33:33
是Linq to Entities,我正在Visual C#下開發一個MVC ASP.NET項目。多樣性OrderForm-AuditTrailLog是1.N – equisde 2013-05-09 12:48:17