Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

How do I write a LINQ to Entities query which has the equivalent of the SQL “in” keyword?

0
Posted

How do I write a LINQ to Entities query which has the equivalent of the SQL “in” keyword?

0

In SQL, you might write a query that looks something like this: SELECT * FROM Foo WHERE blah IN (1, 3, 5, 7). With LINQ to Entities you might have a similar scenario except that you are selecting from an entityset and the list of values you want to compare against is stored in a LIST. Unfortunately, the Entity Framework does not currently support collection-valued parameters. To work around this restriction, you can manually construct an expression given a sequence of values using the following utility method: static Expression> BuildContainsExpression( Expression> valueSelector, IEnumerable values) { if (null == valueSelector) { throw new ArgumentNullException(“valueSelector”); } if (null == values) { throw new ArgumentNullException(“values”); } ParameterExpression p = valueSelector.Parameters.Single(); // p => valueSelector(p) == values[0] || valueSelector(p) == … if (!values.Any()) { return e => false; } var

Related Questions

Thanksgiving questions

*Sadly, we had to bring back ads too. Hopefully more targeted.