LINQ-to-object string.Substring不返回结果

时间:2018-02-12 17:00:50

标签: sql-server vb.net linq

所以我试图在两个对象上进行简单的连接。其中一个对象有一个列有两个字符,另一个我加入我使用子字符串来获取我想要的2个字符。

看起来像这样:

  Dim data As IEnumerable(Of AssignmentsInQCItem)
        Using dbContext = New WebDataEntities()
            Dim inQCRCDs = New String() {"DIR", "IN"}

            data = Await (From m In dbContext.MainDatas
                          Where m.CID = cid AndAlso (m.RCD = "" OrElse m.RCD Is Nothing) AndAlso (dbContext.qcComments.Where(Function(x) x.TransNo = m.TransNo).Any())
                          Join l In dbContext.Lenders
                                 On m.ClientID Equals l.ClientID
                          Select New AssignmentsInQCItem With {
                                 .TransNo = m.TransNo,
                                 .IsRechase = If(m.RechaseON = "R", True, False),
                                 .OrderDate = m.OrderDate,
                                 .DueDate = m.DueDate,
                                 .ClientID = m.ClientID,
                                 .ClientName = l.Name,
                                 .Status = If(inQCRCDs.Contains(m.inQC) = True, "INQC", "Pending"),
                                 .MostRecentComment = m.qcComments.OrderByDescending(Function(x) x.Comment_Date).FirstOrDefault().Comments
                                 }).ToListAsync()

        End Using

        Using dbAdminDBContext = New DBA_AdminEntities()          
            Return (From d In data   
                    Join c In dbAdminDBContext.tbl_ClientPortal_LU.ToList()
                       On c.ClientID_2Char Equals d.ClientID.Substring(0,2)
                    Select New AssignmentsInQCItem With {
                             .TransNo = d.TransNo,
                             .IsRechase = d.IsRechase,
                             .OrderDate = d.OrderDate,
                             .DueDate = d.DueDate,
                             .ClientID = d.ClientID,
                             .ClientName = d.ClientName,
                             .Status = d.Status,
                             .MostRecentComment = d.MostRecentComment,
                             .ClientType = If(c.AKA = "FC", ClientType.FC, If(c.AKA = "LM", ClientType.LM, ClientType.RPI))
                                 }).ToList()
        End Using

然而,这会给我返回0结果,即使仔细观察,子字符串也会返回正确的结果。

更令人困惑的是,如果我在SQL中编写一个简单的查询,它可以正常工作:

SELECT ClientID from WebData..MainData 
inner join DBA_Admin..tbl_ClientPortal_LU
on SUBSTRING(clientid, 0,3) = ClientID_2Char
where cid = '11R' and (RCD= '' or RCD is null)

示例ClientIDs:

MC0099,LM0033,HJ0092

示例ClientID_2Chars: MC,LM,JH

我也有点困惑为什么SQL子字符串带有0,3

无论如何,我知道我之前的查询返回数据。但是联接并没有返回数据。

我的代码出了什么问题?

编辑:

更奇怪的是,这会返回数据:

   Dim db2Chars = dbAdminDBContext.tbl_ClientPortal_LU.Select(Function(x) x.ClientID_2Char).ToList()
    Dim ex2Chars = data.Select(Function(x) x.ClientID.Substring(0, 2)).ToList()
    Dim joi = (From d In db2Chars
               Join e In ex2Chars
                  On d Equals e
               Select d).ToList()

1 个答案:

答案 0 :(得分:0)

   Dim data As IEnumerable(Of AssignmentsInQCItem)
        Using dbContext = New WebDataEntities()
            Dim inQCRCDs = New String() {"DIR", "IN"}

            data = Await (From m In dbContext.MainDatas
                          Where m.CID = cid AndAlso (m.RCD = "" OrElse m.RCD Is Nothing) AndAlso (dbContext.qcComments.Where(Function(x) x.TransNo = m.TransNo).Any())
                          Join l In dbContext.Lenders
                                 On m.ClientID Equals l.ClientID
                          Select New AssignmentsInQCItem With {
                                 .TransNo = m.TransNo,
                                 .IsRechase = If(m.RechaseON = "R", True, False),
                                 .OrderDate = m.OrderDate,
                                 .DueDate = m.DueDate,
                                 .ClientID = m.ClientID,
                                 .ClientName = l.Name,
                                 .Status = If(inQCRCDs.Contains(m.inQC) = True, "INQC", "Pending"),
                                 .MostRecentComment = m.qcComments.OrderByDescending(Function(x) x.Comment_Date).FirstOrDefault().Comments
                                 }).ToListAsync()
        End Using

        Using dbAdminDBContext = New DBA_AdminEntities() 
            Dim db2Chars = dbAdminDBContext.tbl_ClientPortal_LU.Select(Function(x) New With {x.ClientID_2Char, x.AKA}).ToList()
            Return (From d In data
                    Join c In db2Chars
                    On c.ClientID_2Char Equals d.ClientID.Substring(0, 2)
                    Select New AssignmentsInQCItem With {
                             .TransNo = d.TransNo,
                             .IsRechase = d.IsRechase,
                             .OrderDate = d.OrderDate,
                             .DueDate = d.DueDate,
                             .ClientID = d.ClientID,
                             .ClientName = d.ClientName,
                             .Status = d.Status,
                             .MostRecentComment = d.MostRecentComment,
                             .ClientType = If(c.AKA = ClientType.FC.ToString(), ClientType.FC, If(c.AKA = ClientType.LM.ToString(), ClientType.LM.ToString(), ClientType.RPI))
                                 }).ToList()
        End Using

我不知道为什么会这样,但确实如此。我所做的只是选择我需要的两列,并将它们放入上一个变量