如何在一个视图中单独显示两个表,并在一个表中显示联接查询?

时间:2019-04-20 05:28:47

标签: c# sql-server asp.net-mvc entity-framework linq

我正在使用C#和Entity Framework在ASP.NET MVC中创建一个仪表板。

我有两个桌子

  1. Tbl_Channel(ID,频道名称)
  2. Tbl_News(ID,Channel_Id,News_Title,News_Description)

当我尝试此查询时:

 public ActionResult Dashboard()
    {
       ShowData model = new ShowData();
       var rs1 = (from c in db.Tbl_Channel select c).ToList();

       var rs2 = (from c in db.Tbl_News
                  join d in db.Tbl_Channel on c.Channel_Id equals d.Id
                  select new
                         {
                              c.Id,
                              c.News_Title,
                              c.News_Description,
                              d.Channel_Name
                         })
                 .OrderByDescending(x => x.Id)
                 .ToList();

       model.tbl_ChannelData = rs1;
       model.tbl_NewsData = rs2;

       return View(model);
}

在行模型中出现以下错误。tbl_NewsData = rs2:

模型类:

public class ShowData
{
    public List<Tbl_Channel> tbl_ChannelData { get; set; }
    public List<Tbl_News> tbl_NewsData { get; set; }
}

错误:

  

无法将类型'System.Collections.Generic.IEnumerable'隐式转换为'System.Collections.Generic.List

我要显示的输出如下:

(Id, Channel_Name, News_Title, News_Description)

3 个答案:

答案 0 :(得分:0)

将查询更改为该查询可能会起作用:

  var query = (from c in db.tblnews 
                 join d in db.tblchannel on c.Channel_Id equals d.Id 
                 Select new Data()
                        {
                            Id = c.Id,
                            News_Title= c.News_Title,
                            News_Description= c.News_Description,
                            Channel_Name= d.Channel_Name
                         })
                .OrderByDescending(x => x.Id)
                .ToList();


return View(query)

创建此类:

public class Data
{
        public int Id{ get; set; }
        public string News_Title{ get; set; }
        public string News_Description{ get; set; }
        public string Channel_Name{ get; set; }
}

答案 1 :(得分:0)

问题的根本原因是:您已经在模型类中定义了属性,如下所示:

public class ShowData
{
    public List<Tbl_Channel> tbl_ChannelData { get; set; }
    public List<Tbl_News> tbl_NewsData { get; set; }
}

但是,您的查询选择了匿名类型:

  select new
         {
             c.Id,
             c.News_Title,
             c.News_Description,
             d.Channel_Name
         }

这会导致错误。您需要做的是实例化模型类上定义的类型的对象,例如

  select new Tbl_News
         {
             c.Id,
             c.News_Title,
             c.News_Description,
             d.Channel_Name
         }

或者如果该匿名类型与任何现有类型都不匹配,则需要定义合适的类型

public class ChannelAndNews
{
     public int ChannelId { get; set; }
     public string NewsTitle { get; set; }
     public string NewsDescription { get; set; }
     public string ChannelName { get; set; }
}

并创建该类型的实例,然后可以将其分配给模型类属性:

select new ChannelAndNews
         {
             ChannelId = c.Id,
             NewsTitle = c.News_Title,
             NewsDescription = c.News_Description,
             ChannelName = d.Channel_Name
         }

答案 2 :(得分:0)

您的预期结果:[仅假设] [Tables & Data] 1 需要的课程:[就您而言]

public class Tbl_Channel
{
    public int Id { get; set; }
    public string Channel_Name { get; set; }
}

public class Tbl_News
{
    public int Id { get; set; }
    public int Channel_Id { get; set; }
    public string News_Title { get; set; }
    public string News_Description { get; set; }
}

public class ChannelsAndNews
{
    public int ChannelId { get; set; }
    public string Channel_Name { get; set; }
    public string News_Title { get; set; }
    public string News_Description { get; set; }
}

public class ShowData
{
    public List<Tbl_Channel> Channels { get; set; }
    public List<Tbl_News> News { get; set; }
    public List<ChannelsAndNews> ChannelsAndNews { get; set; }
}

控制器方法:[更改]

public ActionResult ShowData()
    {            
        // GetChannels() & GetNews()  -- Seeded Locally in your case from DB.
        var showDataModel = new ShowData();
        showDataModel.Channels = (from channel in GetChannels() select channel).ToList();
        showDataModel.News = (from news in GetNews() select news).ToList();
        showDataModel.ChannelsAndNews = (from channel in GetChannels()
                                         join news in GetNews() on channel.Id equals news.Channel_Id
                                         select new ChannelsAndNews
                                         {
                                             ChannelId = channel.Id,
                                             Channel_Name = channel.Channel_Name,
                                             News_Title = news.News_Title,
                                             News_Description = news.News_Description
                                         })
                                         .OrderByDescending(channelnews => channelnews.ChannelId)
                                         .ToList();
        return View(showDataModel);
    }

这似乎工作正常。