Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to create a constant value - only primitive types

Tags:

c#

linq

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(); 
like image 727
Anton Putov Avatar asked Jun 02 '12 12:06

Anton Putov


1 Answers

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).

like image 95
Raphaël Althaus Avatar answered Oct 04 '22 11:10

Raphaël Althaus