【问题标题】:C# Linq to SQL: The type of one of the expressions in the join clause is incorrect. Type inference failedC# Linq to SQL:连接子句中的表达式之一的类型不正确。类型推断失败
【发布时间】:2014-11-14 13:59:15
【问题描述】:

我正在编写关于 linq to SQL 的连接查询。我在 join 关键字上收到错误,“错误 14 连接子句中的表达式之一不正确。类型推断在调用 'Join' 时失败。”请你帮助我好吗?如果还有其他需要,请告诉我。

var query = (from fd in dbcDefaulter.Fees_Dues
                         join sd in dbcDefaulter.Student_Details on fd.Student_ID equals sd.Student_ID
                         orderby fd.Student_ID
                         select new {  fd.Month }).ToList();

【问题讨论】:

  • Student_Details.Student_ID 是什么数据类型? Fees_Dues.Student_ID 是什么类型?我猜一个是数字类型,另一个是字符串类型。
  • 感谢您的快速回复。我正在检查并尽快更新您。
  • 完全是 Student_Details.Student_ID.Student_ID is int 类型,而另一个是 nvarchar。你能告诉我如何在这里排版吗?
  • 我已经使用Convert.ToInt32(fd.Student_ID) 进行字体大小写了,可以吗?它现在没有给出任何错误。

标签: c# linq linq-to-sql


【解决方案1】:

如果在比较之前将 int 的 id 转换为 string 会怎样?

var query = (from  fd in dbcDefaulter.Fees_Dues
                 join sd in dbcDefaulter.Student_Details 
                 on fd.Student_ID.ToString() equals sd.Student_ID
                 orderby fd.Student_ID
                 select new {  fd.Month }).ToList();

【讨论】:

  • 您也可以尝试在另一个 (nvarchar) Id 上执行 Convert.ToInt32,但这可能会引发异常,而 ToString() 不会。
猜你喜欢
  • 1970-01-01
  • 2012-04-25
  • 1970-01-01
  • 1970-01-01
  • 2013-01-23
  • 1970-01-01
  • 2014-04-14
相关资源
最近更新 更多