实体到linq where子句

时间:2015-05-20 09:06:08

标签: vb.net linq

我正在使用实体来linq

请找我的linq查询 -

 Using lcntxRT60Entities As New RT60Entities(EntityConnection)

            Dim lstrDocumentWhereClause As String
            lstrDocumentWhereClause = astrDocumentWhereClause & " AND it.TransactionType = " + aintTransactionType.ToString + " AND it.VoidIndicator = 0"
            lstrDocumentWhereClause = lstrDocumentWhereClause & " AND it.RtRouteDetails.RouteNumber = " + aintRouteNumber.ToString

            If adatFromDate.ToString IsNot Nothing And adatToDate.ToString IsNot Nothing Then
                adatToDate = adatToDate.AddHours(23).AddMinutes(59).AddSeconds(59)
                lstrDocumentWhereClause = lstrDocumentWhereClause & " AND (it.TransactionDateTime BETWEEN DATETIME'" & Convert.ToDateTime(adatFromDate).ToString("yyyy-MM-dd HH:mm") & "' AND DATETIME'" & Convert.ToDateTime(adatToDate).ToString("yyyy-MM-dd HH:mm:ss") & "')"
            End If

            Dim Documents = From CTH In lcntxRT60Entities.RtCstTrnHeader.Where(lstrDocumentWhereClause) _
                            Join C In lcntxRT60Entities.RtCustomer.Where(astrCustomerWhereClause) On CTH.RtCustomer.CustomerID Equals C.CustomerID _
                            Select CTH, C _
                            Order By CTH.RouteKey, CTH.VisitKey, CTH.TransactionKey

            Dim DocumentDetails = From Document In Documents.ToList _
                                  Select Document.CTH

            Return DocumentDetails.Skip(aintStartRow).Take(aintPageSize).ToList

我的where子句有问题 -

  lstrFilterCriteria = "it.DocumentPrefix + it.DocumentNumber LIKE '" & lstrValue.ToString().Trim() & "'"

此抛出转换错误。

如果我使用这个

lstrFilterCriteria = "it.DocumentPrefix+SqlFunctions.StringConvert((integer)it.DocumentNumber) LIKE '" & lstrValue.ToString().Trim() & "'"

抛出语法错误。

如何转换连接这两列。 文档前缀是字符串,文档编号是整数。 请建议。 Thnaks

1 个答案:

答案 0 :(得分:0)

Using lcntxRT60Entities As New RT60Entities(EntityConnection)
Dim query=lcntxRT60Entities.RtCstTrnHeader
    .Where(t=>t.TransactionType==aintTransactionType)
    .Where(t=>t.VoidIndicator==0)

If adatFromDate.ToString IsNot Nothing And adatToDate.ToString IsNot Nothing Then
  adatToDate = adatToDate.AddHours(23).AddMinutes(59).AddSeconds(59)
  query=query.Where(t=>t.TransactionDateTime>adatFromDate && t.TransactionDateTime<adatToDate);
End If

query=query
  .OrderBy(t=>t.RouteKey)
  .ThenBy(t=>t.Visitkey)
  .ThenBy(t=>t.TransactionKey)

Return query.Skip(aintStartRow).ToList

您必须在此之前为astrDocumentWhereClause和astrCustomerWhereClause添加自己的参数,因为您没有在示例代码中提供它。