rakesh mahur
rakesh mahur

Reputation: 41

Convert SQL query to linq in C# with Entity Framework

I need to convert below query to linq in C#. Can someone share a free online tool or if you have good command in linq then please rewrite in C# supportive linq.

select 
    PaymentHolidayReason as Reason, 
    EmploymentStatus, 
    Cast(count(1) as Decimal) as Count, 
    Cast((select count(1) 
          from MortgageApplications MortgageApps
          where PaymentHolidayReason is not null 
            and EmploymentStatus is not null) as Decimal) as Total
from 
    MortgageApplications MortgageApps
where 
    PaymentHolidayReason is not null
group by 
    PaymentHolidayReason, 
    EmploymentStatus

Upvotes: -1

Views: 4383

Answers (2)

rakesh mahur
rakesh mahur

Reputation: 41

i have write below query and it's working as same above sql statement.

var totalCount = _caseManagerContext.MortgageApplications.Where(x => x.PaymentHolidayReason != null && x.EmploymentStatus != null).Count();
        var result = from mapp in _caseManagerContext.MortgageApplications
                     where mapp.EmploymentStatus!=null && mapp.PaymentHolidayReason!=null &&
                     mapp.LastModifiedDate >= initialDate && mapp.LastModifiedDate <= finalDate
                     group mapp by new { mapp.PaymentHolidayReason, mapp.EmploymentStatus } into g
                     select new { g.Key.PaymentHolidayReason, g.Key.EmploymentStatus, MyCount = g.Count(), Total=totalCount };

Upvotes: 0

Rithik Banerjee
Rithik Banerjee

Reputation: 467

I would suggest you try to use the LinqPad and refer to the link below: http://www.linqpad.net If not satisfied try: http://www.sqltolinq.com but I prefer LinqPad

Upvotes: 1

Related Questions