3

In my last project i decided to use Entity Framework and it was all going well until i try to get datas with "where in", i got an error.

After a tiny search i ve come up with this post and that post.

This is what i am trying to do

var all = fooelements
              .Where(l=>controlsToGet
                            .Contains(l.Control.Name));

Is there any to handle it with lambda expressions or linq with the Entity Framework ?

Thank you

Community
  • 1
  • 1
Barbaros Alp
  • 6,405
  • 8
  • 47
  • 61
  • "Unable to create a constant value of type 'System.Collections.Generic.IEnumerable`1'. Only primitive types (for instance Int32, String and Guid) are supported in this context." – Barbaros Alp Aug 26 '09 at 15:59

2 Answers2

3

Building upon the previous answer I have a tip that makes it easy to do this here:

Tip 8 - How to write 'WHERE IN' style queries using LINQ to Entities

Hope this helps

Alex James

Program Manager - Entity Framework Team

Entity Framework Tips

Alex James
  • 20,874
  • 3
  • 50
  • 49
1

I don't know of a way to generate a WHERE IN clause with EF, but you can use expression trees to construct a WHERE clause that will test for each of your values:

    Expression<Func<FooEntity, bool>> seed = l => false;
    var predicate = controlsToGet.Aggregate(seed,
        (e, c) => Expression.Lambda<Func<DataEventAttribute, bool>>(
            Expression.OrElse(
                Expression.Equal(
                    Expression.Property(
                        Expression.Property(
                            e.Parameters[0],
                            "Control"),
                        "Name"),
                    Expression.Constant(c)),
                e.Body),
            e.Parameters));

    var all = fooelements.Where(predicate);

If you print out predicate, you should see an expression like this:

l => ((l.Control.Name = ctrl5) || l.Control.Name = ctrl4 || ... || False )
dahlbyk
  • 75,175
  • 8
  • 100
  • 122