【问题标题】:c# lambda expression error...this dictionary requires a model item of typec# lambda 表达式错误...此字典需要类型的模型项
【发布时间】:2011-11-30 22:19:15
【问题描述】:

当我更改查询时出现错误。你能帮忙吗?

    public ActionResult Index(int? id)
    {
        Models.MyProjectEntities entity = new Models.MyProjectEntities();

         // NORMAL QUERY, NO PROBLEM
         //var Messages = entity.Message.Where(x => x.Active);

         // JOINED QUERY, GENERATES ERROR
         var Messages = entity.Message.Join(entity.Categories, 
                            m => m.CategoriID,
                            k => k.CategoriID,
                            (m, k) => new { Message = m, Categories = k })
                            .Where(x => x.Message.Active);


        return View(Messages);
    }

这是aspx文件的第一行

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MyProject.Models.Message>>" %>

这是错误

The model item passed into the dictionary is of type 'System.Data.Objects.ObjectQuery`1[<>f__AnonymousType7`2[MyProject.Models.Message,MyProject.Models.Categories]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[MyProject.Models.Message]'.

【问题讨论】:

    标签: c# asp.net-mvc lambda


    【解决方案1】:

    您的视图被声明为采用一组Messages。
    您正在尝试向其传递一组匿名类型。

    正如错误明确指出的那样,它不适合。

    相反,您应该创建一个类来保存来自连接的数据,然后将视图声明为采用该类的集合。
    (视图不能轻易使用匿名类型的模型)

    【讨论】:

      【解决方案2】:

      因为你加入了你创建了一个匿名类型,你需要这样做

      var Messages = entity.Message.Join(entity.Categories, 
          m => m.CategoriID,
          k => k.CategoriID,
          (m, k) => new { Message = m, Categories = k })
          .Where(x => x.Message.Active)
          .Select(x => new Message { ... .. ..  } );
      

      【讨论】:

      • .Select(x => new MyProject.Models.Message { });
      【解决方案3】:
      public ActionResult Index(int? id)
      {
          Models.MyProjectEntities entity = new Models.MyProjectEntities();
      
           // NORMAL QUERY, NO PROBLEM
           //var Messages = entity.Message.Where(x => x.Active);
      
           // JOINED QUERY, GENERATES ERROR
           var Messages = entity.Message.Join(entity.Categories, 
                              m => m.CategoriID,
                              k => k.CategoriID,
                              (m, k) => new { Message = m, Categories = k })
                              .Where(x => x.Message.Active)
                              .Select(x => x.Message);
      
      
          return View(Messages);
      }
      

      或者如果您也需要类别,您应该更改视图模型类型

      编辑:

      public ActionResult Index(int? id)
      {
          Models.MyProjectEntities entity = new Models.MyProjectEntities();
      
           // NORMAL QUERY, NO PROBLEM
           //var Messages = entity.Message.Where(x => x.Active);
      
           // JOINED QUERY, GENERATES ERROR
           var Messages = entity.Message.Join(entity.Categories, 
                              m => m.CategoriID,
                              k => k.CategoriID,
                              (m, k) => new MessageWithCategories { Message = m, Categories = k })
                              .Where(x => x.Message.Active);
      
      
          return View(Messages);
      }
      

      且模型类型应为 MessageWithCategories(必须创建此类)

      【讨论】:

      • 但我在同一个视图中同时需要它们两个
      • 你必须创建这个类。
      • 你可以把这篇文章作为答案表达你的感激之情:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-06
      • 2020-09-08
      • 1970-01-01
      • 2022-01-16
      • 2013-10-18
      • 2015-09-19
      • 1970-01-01
      相关资源
      最近更新 更多