30

Two simple queries - the exception occurs in :

matchings.Any(u => product.ProductId == u.ProductId)

What is wrong? If I write true instead it all is good.

var matchings = (from match in db.matchings 
                 where match.StoreId == StoreId 
                 select match).ToList();

var names = (from product in db.Products
             where matchings.Any(u => product.ProductId == u.ProductId)
             select product).ToList();
Anton Putov
  • 1,951
  • 8
  • 34
  • 62
  • 4
    What is the exception message? Can you post the entire message? – Mark Byers Jun 02 '12 at 13:00
  • 4
    What kind of linq? -to-sql? EF? nhibernate? – CodesInChaos Jun 02 '12 at 13:08
  • Related: [LINQ, Unable to create a constant value of type XXX. Only primitive types or enumeration types are supported in this context](http://stackoverflow.com/q/13405568/456814). –  Oct 13 '15 at 11:51
  • Related: [Unable to create a constant value of type Only primitive types or enumeration types are supported in this context](http://stackoverflow.com/q/18929483/456814). –  Oct 13 '15 at 11:52

4 Answers4

52

First way :

Remove ToList() in the first query.

Or

//instead of retrieving mathings List, retrieve only the productIds you need (which are a List of Primitive types)
var productIdList = db.matchings
.Where(m => m.StoreId == StoreId)
.Select(x => x.ProductId)
.ToList();

var products = db.Products
.Where(p => productIdList
           .Contains(p.ProductId))
.ToList();

Or

//other way
var produts = db.Products
             .Where(p => db.matchings
                        .Any(m => m.StoreId == StoreId && 
                             m.ProductId == p.ProductId)
                    )
             .ToList();

Because I think you're in linq2entities, and you're using a List of Matchings in a query which is not possible (the title of your topic tend to make me believe that's your problem).

Trevor Pilley
  • 16,156
  • 5
  • 44
  • 60
Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122
  • 1
    thanks Raphael - all works great but could you explain why I have null reference problem -> should i always do double chek x == y && y == x ? – Anton Putov Jun 02 '12 at 13:16
  • hmmm, I updated, was the first version ok for you ? In fact, the problem you faced was not a NRE problem. – Raphaël Althaus Jun 02 '12 at 13:18
  • so I put it back. but the first explain maybe better your problem : you tried to used a List (enumerated) in a linq2entities query, which is not permitted. Maybe you could even retry your base code just without the ToList() on your "var matchings" query. – Raphaël Althaus Jun 02 '12 at 13:21
  • yes, if first query without ToList() method my querys work good too.Thanks a lot!!! – Anton Putov Jun 02 '12 at 13:26
7

This looks like a place to use join

 var query =
    from product in db.Products
    join matching in db.Matchings
    on product.ProductId equals matching.ProductId into matchGroup
    where matchGroup.Count() > 0 and matching.StoreId == StoreId
    select product;
agent-j
  • 27,335
  • 5
  • 52
  • 79
3

I was facing the same issue when writing the following query using collection and EF tables:

var result = (from listItem in list
              join dbRecord in Context.MY_TABLE
                  on listItem.MyClass.ID equals dbRecord.ID
              select new { dbRecord, listItem.SomeEnum }).ToList();

I could solve it by changing the source order in in:

var result = (from dbRecord in Context.MY_TABLE
              join listItem in list
                  on dbRecord.ID equals listItem.MyClass.ID
              select new { dbRecord, listItem.SomeEnum }).ToList();
RMalke
  • 4,048
  • 29
  • 42
1

You can try the following. It has worked for me as my ProductId os of type nullable.

IQueryable<matchings> data = db.matchings.Any(u => product.ProductId.Value == u.ProductId);

or this

IQueryable<matchings> data = db.matchings.Any(u => product.ProductId.Value.Equals(u.ProductId));

This worked in my case as the target id is a nullable type as it points to a 1:0 -> * relationship.

Ben Pretorius
  • 4,079
  • 4
  • 34
  • 28