在列表中包含最新注释和注释

时间:2017-08-31 20:24:31

标签: c# asp.net-mvc linq controller

我必须将最新的NoteComment包含在我传递到列表视图的视图模型中,但我无法弄清楚如何将其包含在视图模型中。

我尝试做包含但是在我输入p之后,它不会给我MinimumProductInfo的{​​{1}}属性列表,ProductNotes是它的属性。

这是我正在尝试的控制器代码:

public ActionResult MinimumProductInfoList()
{
     var Model = _minimumProductInfo.GetAll();

     var Notes = db.ProductNotes.Where(p => p.NoteTypeFlag == "p").OrderByDescending(p => p.NoteDate).First();
     var Comments = db.ProductNotes.Where(p => p.NoteTypeFlag == "c").OrderByDescending(p => p.NoteDate).First();

     var Model = db.MinimumProductInfo.Include(p => p)

     var ViewModel = Model.Select(x => new ProductInfoWithNoteList { MinimumProductInfoID = x.MinimumProductInfoID, ItemCode = x.ItemCode, MinimumOnHandQuantity = x.MinimumOnHandQuantity, MaximumOHandQuantity = x.MaximumOHandQuantity, MinimumOrderQuantity = x.MinimumOrderQuantity, LeadTimeInWeeks = x.LeadTimeInWeeks  });

     return View(ViewModel);
}

除了现在我需要将最新的注释和最新评论包含在我的viewmodel

这就是我现在使用WithMetta的帮助:

public ActionResult MinimumProductInfoList()
    {
        var productInfoViewModelCollection =
            from x in db.MinimumProductInfo
            let pnote =
                (from inner_pnote in db.ProductNotes
                 where x.MinimumProductInfoID == inner_pnote.MinimumProductInfoID
                     && inner_pnote.NoteTypeFlag == "p"
                 orderby inner_pnote.NoteDate
                 select inner_pnote).FirstOrDefault()
            let cnote =
                (from inner_cnote in db.ProductNotes
                 where x.MinimumProductInfoID == inner_cnote.MinimumProductInfoID
                      && inner_cnote.NoteTypeFlag == "c"
                 orderby inner_cnote.NoteDate
                 select inner_cnote).FirstOrDefault()
            select new ProductInfoWithNoteList
            {
                MinimumProductInfoID = x.MinimumProductInfoID,
                ItemCode = x.ItemCode,
                MinimumOnHandQuantity = x.MinimumOnHandQuantity,
                MaximumOHandQuantity = x.MaximumOHandQuantity,
                MinimumOrderQuantity = x.MinimumOrderQuantity,
                LeadTimeInWeeks = x.LeadTimeInWeeks,
                Comment = cnote.ToString(),
                PermanentNote = pnote.ToString()
            };

        return View(productInfoViewModelCollection);
    }

1 个答案:

答案 0 :(得分:1)

使用LINQ可能是这样的。

var productInfoViewModelCollection =
    from x in db.MinimumProductInfo
    where x != null
    let pnote =
        (from inner_pnote in db.ProductNotes 
        where inner_pnote != null
            && x.MinimumProductInfoID == inner_pnote.MinimumProductInfoID
            && inner_pnote.NoteTypeFlag == "p"
        orderby inner_pnote.NoteDate descending
        select inner_pnote).FirstOrDefault()
    let cnote =
        (from inner_cnote db.ProductNotes 
        where inner_cnote != null
            && x.MinimumProductInfoID == inner_cnote.MinimumProductInfoID 
            && inner_cnote.NoteTypeFlag == "c"
        orderby inner_cnote.NoteDate descending
        select inner_cnote).FirstOrDefault()
    select new ProductInfoWithNoteList { 
        MinimumProductInfoID = x.MinimumProductInfoID, 
        ItemCode = x.ItemCode, 
        MinimumOnHandQuantity = x.MinimumOnHandQuantity, 
        MaximumOHandQuantity = x.MaximumOHandQuantity, 
        MinimumOrderQuantity = x.MinimumOrderQuantity, 
        LeadTimeInWeeks = x.LeadTimeInWeeks,
        Comment = cnote,
        PermanentNote = pnote
     };