Linq Select 语句不起作用

发布于 2024-11-16 03:57:38 字数 737 浏览 1 评论 0原文

运行以下查询时出现以下错误

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何止钟意 2024-11-23 03:57:38

如果您要比较 pin.TerminalID.Equals((int)terminalId) 中的 ints,请在查询之前进行强制转换并使用 ==

假设 terminalId 是 int

pins.TerminalID == terminalId

我不明白为什么要将 dateNow 转换为 DateTime 它不需要,因为它已经是 DateTime

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.

可是我不能没有你 2024-11-23 03:57:38

我在这里注意到了几件事。

  1. 您已经使用了 pinpin,但我认为它们应该是相同的标识符。这可能只是将代码复制到问题中时出现的拼写错误。

  2. 您进行了一些不必要的显式转换,并且您使用的是 Equals 方法,而不仅仅是 ==。我不确定你为什么要这样做。 Equals 对于 Int32 被覆盖,因此它应该与使用 == 运算符相同;它应该工作正常——但我不确定这个错误可能来自哪里。 LINQ to Entities 可能无法支持将 Int32.Equals(int) 推送到 SQL 查询,尽管它很好地支持 Int32.==

我能想到的唯一其他可能性是 pin.TerminalIDpin.PinExpireDateTime 可能不是您认为的确切数据类型,但您必须检查一下你自己。

不管怎样,你至少可以简化你的代码:

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;
}

Couple things I notice here.

  1. You've used both pins and pin, but I think they're supposed to be the same identifier. This could just be a typo from copying your code into your question.

  2. 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 pushing Int32.Equals(int) to a SQL query, even though it supports Int32.== 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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文