Need to pass 2 lists to MVC view

New to MVC and LINQ

I need to pass two lists to an mvc5 razor view.  Both lists originate from Models retrieve from and Entity Frameworks model (edmx)

I want to create a LINQexpression to select only specific records

Here is the model (class) that I want to pass in

----------------------------------
    public class MyViewModel
    {
        public List<Article> ArticleList { get; set; }
        public List<Advertisement> AdvertisementList { get; set; }
    }
------------------------------------

Here is the controller code
-----------------------------------

            var ArticleList = db.Articles.ToList();

            var AdvertisementList =
                                   (from s in db.Advertisements
                                     where s.AdvertisementID < 3
                                     select new
                                     {
                                         s.AdvertisementID,
                                         s.AdvertisementText,
                                         s.AdvertisementAlt,
                                         s.AdvertisementImageLocation,
                                     }).ToList();

            MyViewModel mvm = new MyViewModel();
            mvm.ArticleList = ArticleList;
            mvm.AdvertisementList = AdvertisementList; <------------------------ Error Here
           
           return View(mvm);

The error I get is Error      1      Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<Info.Models.Advertisement>'      C:xxxxx\Info\Controllers\HomeController.cs      37      37      Info


I get it that AdvertisementList was not a type of Advertisement it was an anonymous type declared from       var AdvertisementList = .....


I also tried this

 var AdvertisementList = db.Advertisements.Where(s => s.AdvertisementID < 3);

My question is how do I use LINQ or whatever to get my SELECTED list of Advertisements from the controller to the view??
Charles BaldoSoftware DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
The problem is that in your LINQ query you're not selecting a new Advertisement object; rather you are selecting a new anonymous object. Anonymous objects are their own type, and you cannot implicitly (or explicitly for that matter) convert those types to a type you have defined. You simply need to select "s" since it should already be of type Advertisement:

var AdvertisementList = (from s in db.Advertisements
                         where s.AdvertisementID < 3
                         select s).ToList();

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Charles BaldoSoftware DeveloperAuthor Commented:
Perfect Thank You
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.