在LINQ中考虑这段代码(请关注var list2
):
var list1 = ....... /* This linq doesnt matter. Just for clarify that it is used in the below linq */
var list2 = dba.OrderForm
.Where(q => q.OrderPriority.OrderPriorityID == orderpriorityID
&& q.StockClass.StockClassID == stockclassID
&& dba.AuditTrailLog.Where(log => q.OrderID == log.ObjectID)
.Any(log => log.ColumnInfoID == 486
&& log.OldValue == "2"
&& log.NewValue == "3")
&& dba.AuditTrailLog.Where(log2 => q.OrderID == log2.ObjectID)
.Any(log2 => log2.ColumnInfoID == 487
&& log2.OldValue == "1"
&& log2.NewValue == "2")
&& lista.Contains(q.OrderID));
这样我在list2中有一个属于OrderForm
模型的记录列表。我需要将它传递给另一个名为ViewResult
的模型:
我需要的是获取属于log2.ModificationDate
表的变量AuditTrailLog
,但它不包含在OrderForm
模型中
List<ViewResult> vr = new List<ViewResult>();
foreach (OrderForm o in list2)
{
ViewResult r = new ViewResult();
r.NumOrden = o.FormNo;
r.Title = o.Title;
r.Com = o.OrderPriority.Descr;
r.OClass = o.StockClass.Descr;
r.RodT = /* <<------ Here is where I need to assign log2.ModificationDate
vr.Add(r);
}
感谢。
答案 0 :(得分:3)
我理解的是AuditTrailLog
关系在获取数据时为空。并且您希望用相关数据填充它。
您必须Include
此表格如下:
(这意味着你在sql上做join
)
var list2 = dba.OrderForm.Include("AuditTrailLog")...
它们之间的关系很重要。 “一对多”或“多对一”。根据您的关系使用AuditTrailLog
或AuditTrailLogs
。