1

在我的 MVC 3 应用程序中,我使用以下查询:

var items = context.QuoteLines.Include("DaybookVehicles").Where(x => x.EnquiryID == id).GroupBy(x=>x.VehicleID).ToList();
return View(items);

然后在我看来,我在顶部有这个:

@model IEnumerable<myApp.Areas.DayBook.Models.DayBookQuoteLines>

但这会引发错误:

The model item passed into the dictionary is of type
'System.Collections.Generic.List`1[System.Linq.IGrouping`2[System.Int32,myApp.Areas.DayBook
.Models.DayBookQuoteLines]]', but this dictionary requires a model item of type 
'System.Collections.Generic.IEnumerable`1[myapp.Areas.DayBook.Models.DayBookQuoteLines]'.

在这种情况下如何使用 group by?因为我想将我的每个人分组QuoteLines到他们的匹配项VehicleID

编辑

报价线模型:

public class DayBookQuoteLines
{
    [Key]
    public int QuoteLineID { get; set; }

    public int EnquiryID { get; set; }

    public virtual int VehicleID { get; set; }

    public virtual DaybookVehicles Vehicle { get; set; }

    [Required(ErrorMessage = "This field is required.")]
    [Display(Name = "Item Number:")]
    [MaxLength(50)]
    public string ItemNumber { get; set; }

    public string ItemDescription { get; set; }
}

车辆型号:

public class DaybookVehicles
{
    [Key]
    public int VehicleID { get; set; }

    public int EnquiryID { get; set; }

    public virtual ICollection<DayBookQuoteLines> QuoteLines { get; set; }

    public string vrm { get; set; }

    public string make { get; set; }

    public string model { get; set; }

    public string engine_size { get; set; }

    public string fuel { get; set; }
}

我想我的设置是正确的,但是每辆车都可以有一组 QuoteLines 附加到它上面!

4

1 回答 1

4

模型类型与函数返回的类型不同。我认为该视图的合适模型是

@model IEnumerable<IGrouping<int, DayBookQuoteLines>> 

并且查询中不需要 ToList

于 2012-05-08T11:12:37.347 回答