无法创建常量值 - 只有原始类型 [英] Unable to create a constant value - only primitive types

查看:29
本文介绍了无法创建常量值 - 只有原始类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两个简单的查询——异常发生在:

Two simple queries - the exception occurs in :

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

怎么了?如果我改写 true 那就一切都好了.

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();

推荐答案

第一种方式:

删除第一个查询中的 ToList().

Remove ToList() in the first query.

//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();

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

因为我认为您在 linq2entities 中,并且您在查询中使用匹配列表这是不可能的(您的主题标题往往让我相信这是您的问题).

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

这篇关于无法创建常量值 - 只有原始类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆