Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

What I have tried:

I tried many things but didnt work anything.
Posted
Updated 22-Nov-17 2:04am

I don't know where you got that var content line from, but its what is causing your problem. It doesn't have a YearID column in it, only the columns you gave it in that first Select. You don't need that line of code at all.

You just need the second one, modified slightly:
C#
public ActionResult FilterImage(int yearID, int eventID, int branchID)
{
    List<EventRegistrationViewModel> contentModel = db.eventRegistration
       .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);
}
 
Share this answer
 
Does EventRegistrationViewModel have YearID and BranchID properties? You probably need to do your Where before your Select on the eventRegistration direct.

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


We don't have access to your database so it's hard to say for sure.
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900