1

How can I write Linq to Entities to make a where in clause similar to one in SQL Server? The issue is that the field I need to search against is nullable int.

I have following code, but it returns "Unable to create a constant value of type 'System.Collections.Generic.List`1'. Only primitive types ('such as Int32, String, and Guid') are supported in this context."

public IList<WantedInfoView> GetWanted(string idList)
{
    List<int> contactIDList = new List<int>();
    contactIDList = idList.Split(',').Select(n => int.Parse(n)).ToList();

    IEnumerable<WantedInfoView> wantedList = (
       from w in db.Wanteds
       where contactIDList.Contains(w.contact_id)
       select new WantedInfoView
       {
           ...
       }
    );
}
Konrad Morawski
  • 8,307
  • 7
  • 53
  • 91

1 Answers1

1

See Collection-valued parameters with The Entity Framework?

The accepted answer links to Tip 8 - How to write 'WHERE IN' style queries using LINQ to Entities, which is what you are looking for.

WHERE IN clause thread seems to be tackling this very issue as well.

Long story short, you can't use collections as parameters in LINQ to Entities queries. The framework is unable to make an SQL query out of it. There are workarounds, although not very pretty.

Community
  • 1
  • 1
Konrad Morawski
  • 8,307
  • 7
  • 53
  • 91