如何在一个视图中使用连接查询在一个视图中分别显示两个表? [英] How to display two tables seprately in a single view with a join query in one table?

查看:374
本文介绍了如何在一个视图中使用连接查询在一个视图中分别显示两个表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用实体框架在mvc c#中创建一个仪表板



我有两张桌子

1.Tbl_Channel(Id,Channel_Name)

2.Tbl_News(Id,Channel_Id,News_Title,News_Description)



我要显示的输出如下:(Id,Channel_Name, News_Title,News_Description)。





我收到以下错误:

不能隐式转换类型
'System.Collections.Generic.IEnumerable< xxx>'到
'System.Collections.Generic.List< xxx>





我的尝试:



型号类别:

公共类ShowData 
{
public List< Tbl_Channel> tbl_ChannelData {get;组; }
public List< Tbl_News> tbl_NewsData {get;组; }
}





控制器类别:



 public ActionResult Dashboard()
{
ShowData model = new ShowData();
var rs1 =(来自db.Tbl_Channel中的c选择c).ToList();

var rs2 =(来自db.Tbl_News中的c在c.Channel_Id上的db.Tbl_Channel中加入d
等于d.Id选择新的
{
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);
}

解决方案

})> OrderByDescending(x => x .Id)。ToList(); 





应该是。,而不是>



调用ToList的唯一原因是确保查询在过期之前运行。我没有理由不去工作。可能使用动态问题?如果你创建一个要使用的类怎么办?



否则,只需使用foreach制作自己的列表,并至少开始过滤问题


i am creating a dashboard in mvc c# using entity framework

I have two tables
1.Tbl_Channel(Id,Channel_Name)
2.Tbl_News(Id,Channel_Id,News_Title,News_Description)

the output i want to display be like : (Id,Channel_Name,News_Title,News_Description).


I got the following error :

Cannot implicitly convert type 
                 'System.Collections.Generic.IEnumerable<xxx>' to 
                    'System.Collections.Generic.List<xxx> 



What I have tried:

Model Class:

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



Controller Class :

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

解决方案

})>OrderByDescending(x=>x.Id).ToList();



That should be a ., not a >

The only reason to call ToList is to ensure that the query runs before it goes stale. There's no reason for that not to work that I can see. Might using a dynamic be the issue? What if you create a class to use?

Otherwise, just use foreach to make your own list and at a minimum, start to filter down what the issue is


这篇关于如何在一个视图中使用连接查询在一个视图中分别显示两个表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆