无法创建一个恒定值 - 只有基本类型 [英] Unable to create a constant value - only primitive types

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

问题描述

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

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

什么是错的?如果我写真正相反,它都是不错的。

  VAR的匹配=(从比赛中db.matchings
                 其中,match.StoreId == STOREID
                 选择匹配).ToList();变种名称=(从db.Products产物
             其中,matchings.Any(U => product.ProductId == u.ProductId)
             选择产品).ToList();


解决方案

第一种方式:

删除了ToList()在第一个查询。

或者

  //而不是检索mathings列表中,仅检索您需要的productIds(这是基本类型的列表)
VAR productIdList = db.matchings
。凡(M = GT; m.StoreId == STOREID)
。选择(X => x.ProductId)
.ToList();变种产品= db.Products
。凡(P => productIdList
           。载有(p.ProductId))
.ToList();

或者

  //别的办法
VAR设备产品= db.Products
             。凡(P => db.matchings
                        。任何(M => m.StoreId == STOREID&放大器;&放大器;
                             m.ProductId == p.ProductId)
                    )
             .ToList();

由于我认为你在linq2entities是,你正在使用匹配数列表中查询这是不可能的(你的专题的标题往往使我相信那是你的问题)。

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

解决方案

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

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

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