检索不属于模型的字段

时间:2013-05-22 10:58:23

标签: c# linq

在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);              
}  

感谢。

1 个答案:

答案 0 :(得分:3)

我理解的是AuditTrailLog关系在获取数据时为空。并且您希望用相关数据填充它。

您必须Include此表格如下:

(这意味着你在sql上做join

var list2 = dba.OrderForm.Include("AuditTrailLog")...

它们之间的关系很重要。 “一对多”或“多对一”。根据您的关系使用AuditTrailLogAuditTrailLogs