Click here to Skip to main content
14,387,532 members
Rate this:
Please Sign up or sign in to vote.
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);
     }
Posted
Updated 19-Apr-19 22:16pm
v3

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

})>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
   
Comments
Umair Nafis 20-Apr-19 3:16am
   
@christian Graus Thank you for the reply sir , here i updated my code kindly review it. im still getting the same error
Christian Graus 20-Apr-19 4:01am
   
You've completely failed to do the things I suggested in order to try to break down the source of the error.....

Stop calling ToList. You have two queries, which one fails? What if you iterate over your IEnumerable and build your own list?
Christian Graus 20-Apr-19 4:01am
   
You also have failed to create an entity class to see if that's the issue.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




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