【问题标题】:LINQ query with GroupBy causing "Unable to create a constant value of type 'System.Object'..."使用 GroupBy 的 LINQ 查询导致“无法创建类型为‘System.Object’的常量值...”
【发布时间】:2013-10-16 19:27:48
【问题描述】:

我尝试对此进行搜索,但似乎找不到任何与我的 LINQ 查询匹配的内容来帮助我解决这个问题。

我在结果视图中的调试器中收到一条消息->基础对象

+base {"无法创建'System.Object'类型的常量值。 仅支持原始类型或枚举类型 上下文。"} System.SystemException {System.NotSupportedException}

这是我的 LINQ 查询(在 LINQPad 中返回结果很好...):

public IEnumerable<PendingItems> GetHazmatPendingShipping()
{
    var pending = context.HazmatInfoes
                         .Where(h => (h.ShippingFlag.Equals(false) && (h.ShippingType.Equals("Manual"))))
                         .GroupBy(x => new {x.ToBU, x.FromBU}, y => new {y})
                         .Select(p => p);
    return pending;
}

我知道我的返回类型是错误的。在我弄清楚为什么这个查询无法返回结果之后,我会继续努力。

我对这个问题的回答:

由于我有一个复合键 {string, string},因此我必须创建一个名为 PendingItems 的类。

public IQueryable<PendingItems> GetHazmatPendingShipping()
    {
        IQueryable<PendingItems> pending = context.HazmatInfoes
            .Where(h => ((h.ShippingFlag.Value == false && h.ShippingType.Equals("Manual"))))
            .GroupBy(x => new {x.ToBU, x.FromBU}, y => y)
            .Select(p => new PendingItems {ToBu = p.Key.ToBU, FromBu = p.Key.FromBU, Items = p});
        return pending;
    }

PendingItems 类:

using System.Collections;
using System.Collections.Generic;

namespace Hazmat.Models
{
    public class PendingItems : IEnumerable
    {
        public string ToBu { get; set; }
        public string FromBu { get; set; }
        public IEnumerable<HazmatInfo> Items { get; set; }

        public IEnumerator GetEnumerator()
        {
            yield return this.Items;           
        }
    }
}

谢谢, 蒂姆

附:这个答案有助于解决这个问题:https://stackoverflow.com/a/1775514/2733668

【问题讨论】:

  • 看起来LINQPad 很烂?
  • 鉴于您的返回类型错误,代码不应编译,那么您如何通过调试器获得运行时错误?
  • 我不知道您为什么说这在LINQPad 中有效,但您的查询有问题
  • @CooperT pending 不是 IEnumerable&lt;PendingItems&gt;,但它是 IEnumerable&lt;IGrouping&lt;SomeAnonymousType, SomeOtherAnonymousType&gt;&gt;
  • @CooperT 你的.Select(p=&gt;p) 是多余的,你的查询显然返回IEnumerable&lt;IGrouping...&gt;

标签: c# linq igrouping


【解决方案1】:

当存在与Nullable&lt;&gt; 字段相关的条件时会发生此错误,并且比较不会反映这一点。然后原始(在您的情况下为false)转换为Nullable&lt;&gt; 对象,并引发异常。

可能ShippingFlag 的类型为Nullable&lt;bool&gt;,并假设您应该像这样重写您的条件:

var pending = context.HazmatInfoes
    .Where(h => (!h.ShippingFlag.HasValue && h.ShippingFlag.Value.Equals(false)) && h.ShippingType.Equals("Manual"))

【讨论】:

  • ShippingFlag 是一个可以为空的类型。我只在h.ShippingType.Equals("Manual") 上查询过,我得到了有效的结果。所以似乎是ShippingFlag 导致了这个问题。我尝试将您添加到查询中,但它仍然失败,我得到相同的错误 Unable to create a constant value of type 'System.Object'. Only primitive types or enumeration types are supported in this context. 如果我添加 h.ShippingFlag.HasValue 但删除 h.ShippingFlag.Equals,查询将返回有效结果。感谢您的洞察力,至少我们现在已经缩小了范围。
  • 确保你打电话给h.ShippingFlag.Value.Equals而不是h.ShippingFlag.EqualsHasValue 只检查 NULL。
  • 需要将h.ShippingFlag.Equals(false) 更改为h.ShippingFlag.Value == false。我应该知道的更好,因为我之前曾以这种方式询问过!谢谢您的帮助!现在来弄清楚如何声明正确的返回类型。
  • 感谢您的回答。它为我节省了很多时间。顺便说一句:我有一个可以为空的 Guid,'guid.Value.Equals' 仍然导致错误,但 'guid.Value ==' 正在工作。也许这对其他人有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-04
  • 2011-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-08
  • 1970-01-01
相关资源
最近更新 更多