Click here to Skip to main content
14,424,412 members

How to display two tables seprately in a single view with a join query in one table?

Umair Nafis asked:

Open original thread
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);
     }
Tags: C#, MVC, entity-framework

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100