English 中文(简体)
LINQ /EF - Using Group 将价值观恢复到观点
原标题:LINQ / EF - Using GroupBy to return values to a View

在我的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] .

我在这种情况下如何使用集团? 由于我愿意将我<代码>QuoteLines的每个条目组合到相应的<编码>上。

Edit

资格模式:

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

我认为,我是正确的,但每辆汽车都能够拿到所附的一批精通品!

最佳回答

模型类型与功能回归类型不同。 我认为,这一观点的适当模式是

@model IEnumerable<IGrouping<int, DayBookQuoteLines>> 

并且不需要ToList在询问中。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签