Linq Select 语句不起作用
运行以下查询时出现以下错误
public int getPinCount(int terminalId, ref int pinnumber)
{
using (var dbEntities = new DatabaseAccess.Schema.BMIEntityModel())
{
DateTime dateNow = DateTime.Now;
return (from pins in dbEntities.PinIds
where pin.TerminalID.Equals((int)terminalId)
&& pin.PinExpireDateTime < (DateTime)dateNow
select pins).Count();
}
return 0;
}
无法创建恒定值 输入“系统.对象”。只是原始的 类型('例如 Int32、String 和 Guid') 在此上下文中受支持。
- TerminalId = int
- PinExpireDateTime = 日期时间
有什么想法吗?
I am getting the following error when running the following query
public int getPinCount(int terminalId, ref int pinnumber)
{
using (var dbEntities = new DatabaseAccess.Schema.BMIEntityModel())
{
DateTime dateNow = DateTime.Now;
return (from pins in dbEntities.PinIds
where pin.TerminalID.Equals((int)terminalId)
&& pin.PinExpireDateTime < (DateTime)dateNow
select pins).Count();
}
return 0;
}
Unable to create a constant value of
type 'System.Object'. Only primitive
types ('such as Int32, String, and
Guid') are supported in this context.
- TerminalId = int
- PinExpireDateTime = datetime
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您要比较
pin.TerminalID.Equals((int)terminalId)
中的ints
,请在查询之前进行强制转换并使用==
。假设
terminalId
是 int我不明白为什么要将
dateNow
转换为DateTime
它不需要,因为它已经是DateTime
。If you're comparing
ints
in:pin.TerminalID.Equals((int)terminalId)
cast it before query and use==
.Assuming that
terminalId
is intI dont understand why are u casting
dateNow
toDateTime
it's not needed since it's alreadyDateTime
.我在这里注意到了几件事。
您已经使用了
pin
和pin
,但我认为它们应该是相同的标识符。这可能只是将代码复制到问题中时出现的拼写错误。您进行了一些不必要的显式转换,并且您使用的是
Equals
方法,而不仅仅是==
。我不确定你为什么要这样做。Equals
对于 Int32 被覆盖,因此它应该与使用==
运算符相同;它应该工作正常——但我不确定这个错误可能来自哪里。 LINQ to Entities 可能无法支持将Int32.Equals(int)
推送到 SQL 查询,尽管它很好地支持Int32.==
。我能想到的唯一其他可能性是
pin.TerminalID
或pin.PinExpireDateTime
可能不是您认为的确切数据类型,但您必须检查一下你自己。不管怎样,你至少可以简化你的代码:
Couple things I notice here.
You've used both
pins
andpin
, but I think they're supposed to be the same identifier. This could just be a typo from copying your code into your question.You've got some unnecessary explicit casts, and you're using the
Equals
method instead of just==
. I'm not sure why you're doing that.Equals
is overridden for Int32, so it should be the same as using the==
operator; it should work fine -- but I'm not sure where else this error could come from. It's possible that LINQ to Entities isn't able to support pushingInt32.Equals(int)
to a SQL query, even though it supportsInt32.==
just fine.The only other possibility I can think of is that
pin.TerminalID
orpin.PinExpireDateTime
might not be the exact datatypes you think they are, but you'll have to check that yourself.Either way, you can at least simplify your code: