0

I have an SQL query :

 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.

0

2 Answers 2

1

You could try putting your LINQ query into LINQPad. It's a useful free tool that'll show you the raw SQL generated by your LINQ query to help you debug it. It'll work with both LINQ-to-SQL and Entity Framework queries.

Sign up to request clarification or add additional context in comments.

2 Comments

I used LINQPAD but I can't use it with this query because I am using entites instead of tablename in this query.
only issue is paranthesis or order of where statements.
0

From the first look main difference is: - FTS function contains in SQL query - Method Contains in LINQ query (which is translated into like, not into FTS call)

If you want to use FTS - you should use table valued functions (TVF) or stored procedures (SP). There are some other options (for example, ExecuteQuery), but I don't find them convenient.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.