LINQ连接2个字段进行搜索

时间:2012-06-01 17:23:30

标签: asp.net-mvc vb.net json linq

我正在尝试连接LINQ中的两个字段,以便我可以使用关键字进行过滤。我发现了一个问题here,我认为这是我的答案,但由于某种原因,我得到了0条记录。这应该返回自动完成文本框的JSON结果(当我不连接字段时它会起作用。)

这是我的代码:

Function CostCodeList(ByVal term As String) As ActionResult

        Dim results = From c In db.ORG_CHART_V
                      Let Fullname = CStr(c.COSTCTR_CD & " - " & c.BREADCRUMB)
                      Where Fullname.ToUpper.Contains(CStr(term).ToUpper)
                      Order By Fullname
                      Select New With {.label = Fullname, .id = c.ORG_NODE_ID}

        Return Json(results.ToArray, JsonRequestBehavior.AllowGet)
  End Function

我也在返回时收到此错误:

  

类型'DbQuery上的公共成员'ToArray'(来自VB $ AnonymousType_3(Of。)   String,Integer))'找不到。

在尝试连接两个字段之前,我正在单独搜索它们,成功。但当我连接它们时,似乎我尝试的所有内容都会给我一个错误和/或零记录。

这是一个有效的不同功能:

Function RoleList(ByVal term As String) As ActionResult
        Dim list As New ArrayList
        Dim results As IQueryable(Of JOB_ROLE)

        If IsNumeric(term) Then
            results = From c In db.JOB_ROLE
                      Where CStr(c.JBROLE_NO).StartsWith(term)
        Else
            results = From c In db.JOB_ROLE
                      Where c.JOB_ROLE_NAME.ToUpper.Contains(CStr(term).ToUpper)
        End If

        results = results.OrderBy(Function(e) e.JOB_ROLE_NAME)

        For Each item In results
            list.Add(New With {.label = item.JOB_ROLE_NAME, .id = item.JOB_ROLE_ID})
        Next

        Return Json(list.ToArray, JsonRequestBehavior.AllowGet)
    End Function

1 个答案:

答案 0 :(得分:0)

以下是按预期工作的新功能:

    Function CostCodeList(ByVal term As String) As ActionResult
        Dim list As New ArrayList

        Dim results = db.ORG_CHART_V.Where(Function(e) (CStr(e.COSTCTR_CD) + " - " + e.BREADCRUMB).Contains(CStr(term).ToUpper)).OrderBy(Function(o) o.COSTCTR_CD)

        For Each item In results
            list.Add(New With {.label = item.COSTCTR_CD & " - " & item.BREADCRUMB, .id = item.ORG_NODE_ID})
        Next

        Return Json(list.ToArray, JsonRequestBehavior.AllowGet)
    End Function