2

I'm working with MVC 4 with Linq to SQL and in my controller class I intend to obtain a List from a query that has several tables and show it into the view as a List where M is a model class:

In the controller I have this

 public List<HomeViewModel> GetHomeViewModel()
    {
        dynamic ReservationDay = (from c in hutRes.Clients
                              join r in hutRes.Reservations on c.ID_Client equals r.FK_Client
                              join ht in hutRes.Hut_Types on r.FK_HutType equals ht.ID_Hut_Type
                              join tr in hutRes.Time_Reserves on r.FK_TimeReserve equals tr.ID_Time_Reserve
                              join tc in hutRes.Type_ClientIds on c.FK_TypeClientId equals tc.ID_Type_ClientId
                              where r.DateR == DateTime.Now
                              select new
                              {
                                  r.ID_Reservation,
                                  tc.ClientId,
                                  Number = c.Number_ID,
                                  c.Name,
                                  c.Phone,
                                  time = tr.TimeReserve,
                                  ReservationDate = r.DateR
                              }).ToList();

        return ReservationDay;
    }

    public ActionResult Index()
    {

        return View(GetHomeViewModel());
    }

And in the view side I have this header:

@model List<HutReservation.ViewModel.HomeViewModel>

I'm having this error:

Can not implicitly convert type 'System.Collections.Generic.List<<>f__AnonymousType4 <int,string,short,string,string,string,System.DateTime,byte,string,short,string,string>>' in 'System.Collections.Generic.List<HutReservation.ViewModel.HomeViewModel>'

Can Anybody help me I'm really stuck here :(

2
  • Sorrymy error means: Can not implicity convert type: 'System.Collections.Generic.List<<>f__AnonymousType4<int,string,short,string,string,string,System.DateTime,byte,string,short,string,string>>' in 'System.Collections.Generic.List<HutReservation.ViewModel.HomeViewModel>' Commented Mar 5, 2014 at 18:31
  • 1
    What is the reason of creating anonymous type instead of HomeViewModel? Commented Mar 5, 2014 at 18:44

2 Answers 2

3

Instead of Anonymous type in LINQ use:

select new HomeViewModel 
{
// Set class variables here...
}
Sign up to request clarification or add additional context in comments.

Comments

2

The problem is that a list of your anonymous type can't be converted to a list of HomeViewModel.

Why are you using an anonymous type, anyway? Simply create a new HomeViewModel instance in your query (Select a HomeViewModel instead of an anonymous type).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.