Сложный запрос LINQ из SQL - PullRequest
0 голосов
/ 14 мая 2011

У меня следующий запрос 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();
}

Пожалуйста, предложите решение.

1 Ответ

1 голос
/ 14 мая 2011

В последних 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;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...