2012-05-08 37 views
1

在我的MVC 3应用程序我使用下面的查询将值返回到View:LINQ/EF - 使用的GroupBy

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]'. 

如何在这种情况下,使用一组由?当我想组我的每一个QuoteLines其匹配VehicleID

编辑

QuoteLines型号:

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附加到它!

+3

您使用它首先阅读文档,然后阅读错误代码,然后解释你想要什么,因为查询和@model说完全不同。 – Euphoric

+0

我们需要您的模型来帮助您! –

+0

查询返回(许多)*组*,其中每一个都有一个'VehicleID'和许多'QuoteLines'。您的视图试图简单显示'QuoteLines'。这些是不同类型的数据。决定是否希望您的视图显示* groups *或* lines *,并适当修改您的查询。 – AakashM

回答

4

的模式类型为从该函数返回的类型不同。我认为,这种观点适当的模型是

@model IEnumerable<IGrouping<int, DayBookQuoteLines>> 

而且也没有必要为ToList在查询

+0

只是关于这个问题,为什么第一个值是int?这是由于VehicleID是一个i​​nt值吗? – CallumVass

+1

@BiffBaffBoff是的,这是分组键 –

+0

好的,谢谢你的回答! :) – CallumVass