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?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
JSBach
  • 4,679
  • 8
  • 51
  • 98
  • 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.) –  Aug 21 '12 at 02:29
  • @pst please verify edit, how should I proceed? – JSBach Aug 21 '12 at 02:57

2 Answers2

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.

Community
  • 1
  • 1
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
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...

Tieson T.
  • 20,774
  • 6
  • 77
  • 92