0

I am having some trouble with a linq query I am trying to write.

I have List A of products that have been modified so I am trying to get the list of products from the db to allow me to apply the changes to them.

I have tried 2 different queries

   var query = from p in db.Products
                where products.Select(z => z.id).Contains(p.Id)
                select p;

        var query2 = from p in db.Products where (from o in products
                     select o.id)
                    .Contains(p.Id)
                    select p;

Both attempts are returning an error

base {System.SystemException} = {"Unable to create a constant value of type 'ProjectABC.Models.ProductModel'. Only primitive types ('such as Int32, String, and Guid') are supported in this context."}

What am I doing wrong?

1 Answer 1

2

I had the same problem the other day, seems EF doesn't support Select().Contains() without giving that error. After testing around for a bit, I ended up splitting it up in what in your case would correspond to;

var IDs = products.Select(z=>z.id);
var query = from p in db.Products
            where IDs.Contains(p.Id)
            select p;

which worked well in my case when the "products" collection was in memory anyway (ie a ToList()'ed result from the database)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.