简体   繁体   English

Linq,array.Contains()生成异常:在此上下文中仅支持基本类型(例如Int32,String和Guid')

[英]Linq, array.Contains() generating exception: Only primitive types ('such as Int32, String, and Guid') are supported in this context

I am trying to perform a query using Linq, something I am new to. 我正在尝试使用Linq执行查询,这是我的新手。 I would like to receive an array of IDs and retrieve the products with these IDs. 我想收到一系列ID并检索带有这些ID的产品。 The linq query I got to was this one: 我得到的linq查询就是这个:

    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."} {“无法创建类型'System.Int32 []'的常量值。在此上下文中仅支持基本类型(例如Int32,String和Guid')。”}

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? 有人可以告诉我,如果我的语法错误或者它是否真的是Linq问题? (Operation not supported?) (不支持操作?)

How do I find out which EF version I am using? 如何找出我使用的EF版本?

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. 更新:据我所知,关于ids.Contains()的这个问题将是一个L2S问题,但可能的答案是EF2不支持它。 Am I understanding it correctly? 我理解正确吗?

Entity Framework only began supporting Contains against arrays in EF 4.0. 实体框架仅在EF 4.0中开始支持Contains阵列的Contains You can work around this in older versions by building an Expression to include the list of OR conditions. 您可以通过构建Expression来包含OR条件列表,从而在旧版本中解决此问题。

ps: how do I find out which EF version I am using? ps:我如何找出我正在使用的EF版本?

Check the version of the System.Data.Entity assembly. 检查System.Data.Entity程序集的版本。 Refer to this other question for details. 有关详细信息,请参阅此其他问题

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): 假设pricingInfoRepository是您的对象上下文或返回相关对象集的存储库类,这可能会起作用(它是查询的方法语法版本,稍微调整一下):

 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 int s, which LINQ treats as an IEnumerable , which should be fine. 你在一个int数组上调用Contains() ,LINQ将其视为IEnumerable ,应该没问题。 I've used queries similar to this before without any issues... 我之前使用过与此类似的查询,没有任何问题......

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 此上下文仅支持原始类型(“例如 Int32、String 和 Guid”) - Only primitive types ('such as Int32, String, and Guid') are supported in this context 显示无法创建类型“”的常量值。 在这种情况下,仅支持基本类型(例如Int32,String和Guid) - Showing Unable to create a constant value of type ''. Only primitive types ('such as Int32, String, and Guid') are supported in this context EF-Code First:无法创建类型''的常量值。 在此上下文中仅支持原始类型(例如Int32,String和Guid') - EF-Code First: Unable to create a constant value of type ''. Only primitive types ('such as Int32, String, and Guid') are supported in this context 无法创建类型为“结算类型”的常量值。 在这种情况下,仅支持基本类型(例如Int32,String和Guid) - Unable to create a constant value of type 'Closure type'. Only primitive types ('such as Int32, String, and Guid') are supported in this context 带有期望的 Linq:此上下文中仅支持原始类型或枚举类型 - Linq with expect: Only primitive types or enumeration types are supported in this context LINQ查询。 在此上下文中仅支持基元类型或枚举类型 - LINQ query. Only primitive types or enumeration types are supported in this context Linq to Entities:在此上下文中仅支持原始类型或枚举类型 - Linq to Entities: Only primitive types or enumeration types are supported in this context Linq Any-在此上下文中仅支持原始类型或枚举类型 - Linq Any - Only primitive types or enumeration types are supported in this context 在此上下文中仅支持原始类型或枚举类型。 Linq到SQL - Only primitive types or enumeration types are supported in this context. Linq to SQL 表达式-无法创建类型为'System.Collections.Generic.List`1'的常量值。 仅原始类型(例如Int32,String和Gu - Expression - Unable to create a constant value of type 'System.Collections.Generic.List`1'. Only primitive types ('such as Int32, String, and Gu
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM