【问题标题】:LINQ Find all records where ID contains search termLINQ 查找 ID 包含搜索词的所有记录
【发布时间】:2018-03-12 14:00:02
【问题描述】:

我正在尝试检查是否可以通过 Linq 在 Id 中找到给定的搜索词。

我尝试使用 where(x => x.Id.ToString().Contains(term)) 但这不起作用,因为它给出了不支持 .ToString 的异常

我也尝试使用SqlFunctions.StringConvert((double)x.Id).Contains(term),但效果不佳,因为它给出了异常

'The specified method System.String StringConvert(System.Nullable`1[System.Double]) on the type System.Data.Objects.SqlClient.SqlFunctions cannot be translated to a LINQ to Entities store expression.'

如何在不将所有内容加载到内存的情况下使此查询正常工作(这不是我想要的)

应用程序正在使用 EF 5.0 并连接到 oracle 数据库

SqlFunctions.StringConvert((double?)x.Id) 不起作用。

【问题讨论】:

  • x.Id是什么类型?
  • 你为什么要投到double?似乎需要Nullable<double>
  • ToString 后是否缺少括号?
  • 请注意SqlFunctions.StringConvert 只能采用double? - 但您可以将`int` 转换为double?
  • @Strike08 我已删除重复项,以便您正确回答问题。我还添加了与此处相关的 Oracle 标记。

标签: c# .net oracle entity-framework linq


【解决方案1】:

要使其工作,您需要调用 Oracle 数据库的 To_Char 方法。为此,您需要像这样在 Edmx 中映射函数:

<Function Name="To_Char" ReturnType="varchar2" Aggregate="false" BuiltIn="true" NiladicFunction="false" IsComposable="true" ParameterTypeSemantics="AllowImplicitConversion" StoreFunctionName="To_Char" Schema="{INSERT OWN SCHEMA HERE}">
      <Parameter Name="value" Type="number" Mode="In" />
</Function>

在这之后你需要创建以下类+方法:

public static class OracleFunctions
{
    [EdmFunction("Model.Store", "To_Char")]
    public static string To_Char(decimal input)
    {
        throw new NotImplementedException();
    }
}

当你这样做时,你可以像这样在 where 语句中调用它

.Where(OracleFunctions.To_Char(x.Code).Contains(term))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-26
    相关资源
    最近更新 更多