3

I am trying to perform a query using Linq, something I am new to. I would like to receive an array of IDs and retrieve the products with these IDs. The linq query I got to was this one:

    public Product[] RetrieveProduct(int[] ids)
    {
        var query = from productInfos in productRepository.Table
                    where (ids.Contains(productInfos.Id))
                    && productInfos.Active
                    select new ProductIndiInfo
                    {
                        ProductId = productInfos.Id,
                        CurrentPrice = productInfos.CurrentPrice
                    };
        return query.ToArray();
    }

But I keep receiving the famous exception:

{"Unable to create a constant value of type 'System.Int32[]'. Only primitive types ('such as Int32, String, and Guid') are supported in this context."}

Some resources I read say that it is not available. But some others tell me it should work. I've seen many posts with alternatives here and in blogs and so on but it doesn't work. I am starting to think that this is a syntax issue...

I've also tried this approach:

 var query = from productInfos in pricingInfoRepository.Table
                    where ids.Any(id => id == productInfos.ProductId)
                    && productInfos.Active
                    select new PricingInfo
                    {
                        ProductId = productInfos.Id,
                        CurrentPrice = productInfos.CurrentPrice
                    };
        return query.ToArray();

But I got the same issue.

Could someone please tell me if my syntax is wrong or if it is really a Linq problem? (Operation not supported?)

How do I find out which EF version I am using?

Update: As far as I understood, this question about the ids.Contains() would be a L2S question, but a possible answer would be that EF2 does not support it. Am I understanding it correctly?

2
  • 1
    If EF and not L2S, please re-tag: they are two separate products. If EF please include the version number of the assembly referenced. (View the properties for the referenced assembly.) Commented Aug 21, 2012 at 2:29
  • @pst please verify edit, how should I proceed? Commented Aug 21, 2012 at 2:57

2 Answers 2

3

Entity Framework only began supporting Contains against arrays in EF 4.0. You can work around this in older versions by building an Expression to include the list of OR conditions.

ps: how do I find out which EF version I am using?

Check the version of the System.Data.Entity assembly. Refer to this other question for details.

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

5 Comments

@pst Yeah - but then text specifies "which EF version" - not sure.
@pst I believe that was the EF 1 error message, though, and L2Q has a different error IIRC
@ReedCopsey system data entity v4.0.30319. I should be able to perform the ids.Contains() operation, correct? Is there any syntax error?
@Oscar No - what you have should work, but as you see, it can't be null.
that is true. This was a newbie fault, but maybe the error message could be more specific. Thanks for your help
1

Assuming that pricingInfoRepository is either your object context or a repository class that returns the relevant object set, this may work (it's the method-syntax version of your query, tweaked a bit):

 var query = pricingInfoRepository.Table
            .Where(pi => pi.Active && ids.Contains(pi.Id))
            .Select(pi => new PricingInfo
            {
                ProductId = pi.Id,
                CurrentPrice = pi.CurrentPrice
            })
            .ToArray();

You're calling Contains() on an array of ints, which LINQ treats as an IEnumerable, which should be fine. I've used queries similar to this before without any issues...

1 Comment

just found out... ids is null for some reason, this is causing the error

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.