【发布时间】: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<PendingItems>,但它是IEnumerable<IGrouping<SomeAnonymousType, SomeOtherAnonymousType>>。 -
@CooperT 你的
.Select(p=>p)是多余的,你的查询显然返回IEnumerable<IGrouping...>