Click here to Skip to main content
15,917,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an SQL query :

SQL
SELECT Sum(ABS([Minimum Installment])) AS SumOfMonthlyPayments 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 Status ID] <> 999)
        AND ((tblAccount.[Account Type] NOT IN ('CL01','PL01','CL10','
        CL11','PL10','PL11','OD','CL00','PL00','CL03','CL20','CL30','CL31','CL32',
        'CL33','CL34','CL35','CL69','CL90','ML00','PL03','PL20','PL30','PL31','PL33',
        'CA00', 'CA01', 'CA03', 'CA04', 'CA02', 'PA00', 'PA01', 'PA02', 'PA03', 'PA04',
        'PL34','PL35','PL40','PL90'))
        AND NOT CONTAINS(tblAccount.[Account Type], 'Overdra')
        OR NOT CONTAINS(tblAccount.[Account Type], 'Mortgage')
        OR NOT CONTAINS(tblAccount.[Account Type],'Revolv')
        OR NOT CONTAINS(tblAccount.[Account Type],'*Credit*Card*'))


I have translated it to LINQ:
var excludeTypes = new[]
               {
                  "CA00", "CA01", "CA03", "CA04", "CA02",
                    "PA00", "PA01", "PA02", "PA03", "PA04"      
               };
            var maxStateChangeMonth = 4;
            var excludeStatusId = 999;
            var includOtherPayments = new[] {
                        "CL01","PL01","CL10",
                        "CL11","PL10","PL11","OD","CL00","PL00","CL03","CL20","CL30",
                        "CL31","CL32,CL33","CL34","CL35","CL69","CL90","ML00","PL03",
                        "PL20","PL30","PL31","PL33,CA00", "CA01", "CA03", "CA04","CA02",
                        "PA00", "PA01", "PA02", "PA03", "PA04,PL34","PL35","PL40","PL90" 
                    };
            var sum = (
               from account in context.Accounts
               from owner in account.AccountOwners
               where owner.AccountOwnerRegistryId == ownerRegistryId
               where !excludeTypes.Contains(account.AccountType)
               where account.StateChangeDate == null
               ||
                   EntityFunctions.DiffMonths(account.StateChangeDate, DateTime.Now)
                       <= maxStateChangeMonth
               where includOtherPayments.Contains(account.AccountType) ||
                       !account.AccountType.Contains("Overdra") || !account.AccountType.Contains("Mortgage")
                       || !account.AccountType.Contains("Revolv") || !account.AccountType.Contains("*Credit*Card*")
               where account.AccountStatusId != excludeStatusId
               select (decimal?)account.MinimumInstallment).ToList()
               .Sum(minimumInstallment => Math.Abs((decimal)(minimumInstallment)));
            return sum;

but SQL is returning 0 where as LINq is returning 23456. I know the issue is with paranthesis in LINQ or order of where statements. Please suggest me solution.
Posted
Comments
Silver Lightning 16-May-11 2:45am    
a quite bit on parenthesis

1 solution

try that way

var excludeTypes = new[]
               {
                  "CA00", "CA01", "CA03", "CA04", "CA02",
                    "PA00", "PA01", "PA02", "PA03", "PA04"      
               };
            var maxStateChangeMonth = 4;
            var excludeStatusId = 999;
            var includOtherPayments = new[] {
                        "CL01","PL01","CL10",
                        "CL11","PL10","PL11","OD","CL00","PL00","CL03","CL20","CL30",
                        "CL31","CL32,CL33","CL34","CL35","CL69","CL90","ML00","PL03",
                        "PL20","PL30","PL31","PL33,CA00", "CA01", "CA03", "CA04","CA02",
                        "PA00", "PA01", "PA02", "PA03", "PA04,PL34","PL35","PL40","PL90" 
                    };
            var sum = (
               from account in context.Accounts
               from owner in account.AccountOwners
               where owner.AccountOwnerRegistryId == ownerRegistryId
               where !excludeTypes.Contains(account.AccountType)
               where account.StateChangeDate == null || EntityFunctions.DiffMonths(account.StateChangeDate, DateTime.Now) <= maxStateChangeMonth
               where !(includOtherPayments.Contains(account.AccountType) ||
                       account.AccountType.Contains("Overdra") || 
                       account.AccountType.Contains("Mortgage") || 
                       account.AccountType.Contains("Revolv") || 
                       account.AccountType.Contains("*Credit*Card*"))
               where account.AccountStatusId != excludeStatusId
               select (decimal?)account.MinimumInstallment).ToList()
               .Sum(minimumInstallment => Math.Abs((decimal)(minimumInstallment)));
            return sum;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900