【问题标题】:c# Linq Oracle convert long to string for joinc# Linq Oracle 将 long 转换为字符串以进行连接
【发布时间】:2016-08-09 15:07:03
【问题描述】:

我在尝试在 Oracle 中连接两个表时遇到以下问题。 其中一个表有一个字符串值,另一个是一个 long。

var query = (from d in context.entity
join m in context.entity2
on d.ordernum.Substring(2) equals m.ordernum.ToString())
select new { d.ordernum, d.customer, m.recordkey});
var items = query.ToList();

问题是这不适用于 Oracle

  1. 不能使用 SQLFunctions,因为这是一个 Oracle 数据库

  2. 不能使用 .ToString()

    LINQ to Entities 无法识别方法“System.String ToString()”方法,并且该方法无法转换为存储表达式。

  3. 强制转换使用 long.Parse()

    LINQ to Entities 无法识别方法“Int64 Parse(System.String)”方法,并且该方法无法转换为存储表达式。

任何帮助将不胜感激。

【问题讨论】:

  • ToString 在 LINQ to Entities 中受支持。您使用的是什么版本的 EF?
  • EF 是 6.1.3,如上所示,当我们使用 ToString() 时,会得到 Linq 无法识别该方法的错误。

标签: c# oracle entity-framework linq


【解决方案1】:

您可以使用 AsEnumerable(),因此它将在 C# 中运行并且不会出现这些错误。

var reault = (from d in context.entity.AsEnumerable() 
             join m in context.entity2.AsEnumerable() 
             on d.ordernum.Substring(2) equals m.ordernum.ToString()
             select new 
             { 
                 d.ordernum, 
                 d.customer,
                 m.recordkey
             }).ToList(); 

否则你可以扩展它和write your "own" functions。类似的东西:(未测试)

<Function Name="OracleSubString" ReturnType="Edm.String">
  <Parameter Name="order_number" Type="System.Int64" />
  <Parameter Name="start_index" Type="System.Int32" />
  <DefiningExpression>
   SUBSTR(CAST (order_number as varcahr(100)),start_index, LEN(CAST (order_number as varcahr(100)) - start_index)
  </DefiningExpression>
</Function>

【讨论】:

  • 根据我的常识,我认为他是在尝试获取数据库查询,而不是将所有项目都抓取到内存中。我认为是不可行的。
  • @anotherNeo - 我也更喜欢在数据库中,但据我目前所知,SqlFunctions 是这种方式(但他所说的不支持 Oracle ......)
  • @Denisjc - 哪个函数是AsEnumerable() 或“编写你自己的函数”?
  • 该函数将具有相同的结果,因为您仍然需要一种方法将 long 转换为字符串。那么你会在函数中添加什么来执行演员阵容呢? longValue.ToString() 这将导致同样的问题 The specified method 'System.String LongToString(Int64)' cannot be translated into a LINQ to Entities store expression.
  • @anotherNeo - 对于其他人,那么 SQLFunctions/ 解析应该可以工作...... Oracle .. 我猜不是
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-02
相关资源
最近更新 更多