C# 名称';XYZ';按LINQ查询分组时不存在

C# 名称';XYZ';按LINQ查询分组时不存在,c#,asp.net,linq,razor,asp.net-mvc-5,C#,Asp.net,Linq,Razor,Asp.net Mvc 5,我正在犯错误 按LINQ查询分组时名称“XYZ”不存在 这是我尝试过的行动方法 public ActionResult Gellery() { var list = db.EventGallerys .GroupBy(eg => new { EventId, Title, EventDate, Description, ThumbImage }) .OrderByDescending(eg => eg.Event

我正在犯错误

按LINQ查询分组时名称“XYZ”不存在

这是我尝试过的行动方法

public ActionResult Gellery()
    {
        var list = db.EventGallerys
            .GroupBy(eg => new { EventId, Title, EventDate, Description, ThumbImage })
            .OrderByDescending(eg => eg.EventDate)
            .Take(5)
            .AsEnumerable()
            .Select(eg => new
            {
                EventId = eg.EventId,
                Day = eg.EventDate.ToString("D"),
                Month = eg.EventDate.ToString("MMM"),
                Year = eg.EventDate.ToString("yyyy"),
                Title = eg.Title,
                Description = eg.Description,
                ThumbImage = eg.ThumbImage
            }).ToList();

        return View(list);
     }
下面是我以图像格式显示的错误

以下是我昨天的要求,但仍然没有得到适当的结果

我也尝试了这个查询,但得到了错误

  LINQ to Entities does not recognize the method 'System.String ToString(System.String)' method, and this method cannot be translated into a store expression.

   var list = (from p in db.EventGallerys
                    group p by new { p.EventId, p.EventDate, p.Title, p.Description, p.ThumbImage } into r
                    select new
                    {
                        EventId = r.Key.EventId,
                        Day = r.Key.EventDate.Day,
                        Month = r.Key.EventDate.ToString("MMM"),
                        Year = r.Key.EventDate.Year,
                        Title = r.Key.Title,
                        Description = r.Key.Description,
                        ThumbImage = r.Key.ThumbImage,
                        EventDate = r.Key.EventDate
                    }).OrderByDescending(c => c.EventDate).ToList().Take(5);


          return View(list);
下面是我的返回视图设计

@model List<IBAC.Models.EventGallery>
@{
ViewBag.Title = "Gellery";
}
<br /><br /><br />
<h2>Gellery</h2>


@foreach (var item in Model)
{
  <a href='@Url.Content("~/GalleryImages/" + item.ThumbImage)'>
      <img class='thumbnail' src='@Url.Content("~/GalleryImages/" + item.ThumbImage)' />
</a>

}
@型号列表
@{
ViewBag.Title=“Gellery”;
}



血战画廊 @foreach(模型中的var项目) { }
您是linq
Group by
匿名对象需要为
Group by
值提供属性名称,使用linq
选择时需要使用
获取值

var list = db.EventGallerys.GroupBy(eg => new {
                            EventId = eg.EventId,
                            Title = eg.Title,
                            EventDate = eg.EventDate,
                            Description = eg.Description,
                            ThumbImage = eg.ThumbImage })
                       .OrderByDescending(eg => eg.Key.EventDate)
                       .Take(5)
                       .Select(eg => new
                       {
                           EventId = eg.Key.EventId,
                           Day = eg.Key.EventDate.ToString("D"),
                           Month = eg.Key.EventDate.ToString("MMM"),
                           Year = eg.Key.EventDate.ToString("yyyy"),
                           Title = eg.Key.Title,
                           Description = eg.Key.Description,
                           ThumbImage = eg.Key.ThumbImage
                       }).ToList();
编辑

您需要使用
viewModel
类将数据传送到
View
,因为
EventGallery
此类是
DB
映射器模型

在这种情况下,您可以尝试使用
EventGalleryViewModel
类作为您的razor
@model

public class EventGalleryViewModel
{
    public string EventId { get; set; }
    public string Day { get; set; }
    public string Month { get; set; }
    public string Year { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public string ThumbImage { get; set; }
}
控制器

public ActionResult Gellery()
{
    var list = db.EventGallerys.GroupBy(eg => new {
                            EventId = eg.EventId,
                            Title = eg.Title,
                            EventDate = eg.EventDate,
                            Description = eg.Description,
                            ThumbImage = eg.ThumbImage })
                       .OrderByDescending(eg => eg.Key.EventDate)
                       .Take(5)
                       .Select(eg => new EventGalleryViewModel()
                       {
                           EventId = eg.Key.EventId,
                           Day = eg.Key.EventDate.ToString("D"),
                           Month = eg.Key.EventDate.ToString("MMM"),
                           Year = eg.Key.EventDate.ToString("yyyy"),
                           Title = eg.Key.Title,
                           Description = eg.Key.Description,
                           ThumbImage = eg.Key.ThumbImage
                       }).ToList();

    return View(list);
 }
看法

@型号列表
@{
ViewBag.Title=“Gellery”;
}



血战画廊 @foreach(模型中的var项目) { }
关于:

db.EventGallerys .GroupBy(eg => new { EventId = eg.EventId, Title = eg.Title, EventDate = eg.EventDate, Description = eg.Description, ThumbImage = eg.ThumbImage }); 

ToString(“MMM”)
无法转换为SQL,这就是为什么在第二段代码中出现错误的原因。您可以只做
Month=r.Key.EventDate.Month
请添加您的答案@ChetanRanpariyaList在返回获取此错误时给出了正确的结果:传递到字典中的模型项的类型为“System.Collections.Generic.List
1[f_uAnonymousType2
7[System.String,System.String,System.String,System.String,System.String,System.String,System.String],但此词典需要类型为“System.Collections.Generic.List
1[IBAC.Models.EventGallery]”的模型项,但此词典需要类型为“System.Collections.Generic.List
1[IBAC.Models.EventGallery]”的模型项。并检查我是否有更新我的视图。@D-请检查昨天发布的url:@D-Shih@mazhar我编辑了我的答案,我建议您使用
ViewModel
而不是匿名对象传递到
View
使用
EventId=eg.EventId,
与只使用
eg.EventId有什么好处,
?事实上,我已经阅读了模型类na名称:EventGallery,如上面的url所示,为什么再次查看模型类只是阅读na。你的意思是我必须保留EventGallery类,我必须再添加一个EventGalleryViewmodel类na,因为我非常喜欢mvc5@施德荣
db.EventGallerys .GroupBy(eg => new { EventId = eg.EventId, Title = eg.Title, EventDate = eg.EventDate, Description = eg.Description, ThumbImage = eg.ThumbImage });