2011-05-14 77 views
0

我有以下SQL查询:复杂的LINQ查询从SQL

SELECT Count(*) AS CountOfRecs FROM tblAccount INNER JOIN tblAccountOwner ON 
      tblAccount.[Creditor Registry ID] = tblAccountOwner.[Creditor Registry ID] AND 
      tblAccount.[Account No] = tblAccountOwner.[Account No] WHERE (tblAccountOwner. 
      [Account Owner Registry ID] = 731752693037116688) AND (tblAccount.[Account Type] 
      NOT IN ('CA00', 'CA01', 'CA03', 'CA04', 'CA02', 'PA00', 'PA01', 'PA02', 'PA03', 'PA04')) 
      AND (DATEDIFF(mm, tblAccount.[State Change Date], GETDATE()) <= 6 
      OR tblAccount.[State Change Date] IS NULL) 
     AND ((tblAccount.[Account Type] IN ('OD','CL00','PL00')) OR 
     (tblAccount.[Account Type] LIKE '%Overdra%')) 

,我想将它翻译成LINQ。我创建了以下LINQ,但它不返回相同的计数。 SQL返回2,LINQ返回0.

public int OverDraftCount(long AccountOwnerRegistryId = 731752693037116688) 
{ 
    CreditRegistryContext context = new CreditRegistryContext(); 
    string notAllowedAccountTypes = "CA00, CA01, CA03, CA04, CA02, PA00, PA01, PA02, PA03, PA04"; 
    var subList = notAllowedAccountTypes.Split(','); 
    string AllowedAccountTypes = "OD,CL00,PL00"; 
    var subList1 = AllowedAccountTypes.Split(','); 

    var query = from c in context.AccountOwners 
       .Where(p => p.CreditorRegistryId == p.Account.CreditRegistryId 
        && p.AccountNo == p.Account.AccountNo 
        && p.AccountOwnerRegistryId == AccountOwnerRegistryId 
        && !subList.Contains(p.Account.AccountType) 
        && (EntityFunctions.DiffMonths(
          p.Account.StateChangeDate, DateTime.Now) < 6 
          || p.Account.StateChangeDate == null 
          && (subList1.Contains(p.Account.AccountType) 
          || p.Account.AccountType.Contains("Overdra")))) 
       select c; 
    return query.Count(); 
} 

请建议解决方案。

+0

看起来你的括号分组是不太一样的。此外,对于“DiffMonths”,它应该是“<6”还是“<= 6”? – mellamokb 2011-05-14 17:20:31

+0

@mellamokb其<6和SQL是正确的,问题与LINQ,因为我把这个SQL翻译成LINQ – DotnetSparrow 2011-05-14 17:23:29

回答

1

你的括号是关闭的最后4行Where条款:

var query = from c in context.AccountOwners 
       .Where(p => p.CreditorRegistryId == p.Account.CreditRegistryId 
        && p.AccountNo == p.Account.AccountNo 
        && p.AccountOwnerRegistryId == AccountOwnerRegistryId 
        && !subList.Contains(p.Account.AccountType) 
        && (EntityFunctions.DiffMonths(p.Account.StateChangeDate, DateTime.Now) < 6 
         || p.Account.StateChangeDate == null) 
        && (subList1.Contains(p.Account.AccountType) 
         || p.Account.AccountType.Contains("Overdra"))) 
       select c;