I am getting the following error when running the following query
public int getPinCount(int terminalId, ref int pinnumber)
{
using (var开发者_JAVA技巧 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?
If you're comparing ints
in: pin.TerminalID.Equals((int)terminalId)
cast it before query and use ==
.
Assuming that terminalId
is int
pins.TerminalID == terminalId
I dont understand why are u casting dateNow
to DateTime
it's not needed since it's already DateTime
.
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
or pin.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:
public int getPinCount(int terminalId, ref int pinnumber)
{
using (var dbEntities = new DatabaseAccess.Schema.BMIEntityModel())
{
DateTime dateNow = DateTime.Now;
return (from pin in dbEntities.PinIds
where pin.TerminalID == terminalId
&& pin.PinExpireDateTime < dateNow
select pin).Count();
}
return 0;
}
精彩评论