0

I want to give multiple where condition while binding data in mvc using linq. I tried where condition same as sql query and other type also but didnt work and showing error i.e.

The specified type member 'YearID' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

Here is my code which I tried latest,

 public ActionResult FilterImage(int yearID, int eventID, int branchID)
{
    var content = db.eventRegistration.Select(s => new
    {
        s.EventRegistrationID,
        s.Image,
        s.IsActive
    });

    List<EventRegistrationViewModel> contentModel = content
       .Select(item => new EventRegistrationViewModel() 
       {
            EventRegistrationID = item.EventRegistrationID,
            Image = item.Image,
            IsActive = item.IsActive
       })
       .Where(c => c.IsActive == true && c.YearID == yearID && c.BranchID == branchID)
       .Take(15).ToList();

    return View(contentModel);
}

this code is not working for multiple where condition.

I know and this question is almost asked previous time but then also that answer is didn't work for this solution.

Thank You.

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
s.k.Soni
  • 1,139
  • 1
  • 19
  • 37

1 Answers1

1

I don't know what you are trying but the solution is in content list you must have select YearID and BranchID because in where you are using it.

I don't know you need all properties or not it's different discussion but as of now you can do like below

 public ActionResult FilterImage(int yearID, int eventID, int branchID)
{
    var content = db.eventRegistration.Select(s => new
    {
        s.EventRegistrationID,
        s.Image,
        s.IsActive,
        s.YearID,
        s.BranchID
    }).ToList();

    List<EventRegistrationViewModel> contentModel = content.Select(item => new EventRegistrationViewModel()
    {
        EventRegistrationID = item.EventRegistrationID,
        Image = item.Image,
        IsActive = item.IsActive
    }).Where(c => c.IsActive == true && c.YearID == yearID && c.BranchID == branchID).Take(15).ToList();

    return View(contentModel);
}
Curiousdev
  • 5,668
  • 3
  • 24
  • 38