I am quite new to linq and .net core. I am trying to calculate the next tax return date of a company as a part of my final year's project.
Thats what i have tried in SQL, now i am trying to convert this sql into Linq Query, I need some help to get the desired results.
WITH cte_company (CompanyID, CompanyName, CompanyNumber, IncorporatedDate, TOTAL_YEARS) AS (SELECT CompanyID, CompanyName, CompanyNumber, IncorporatedDate, DATEDIFF(YEAR, IncorporatedDate, CURRENT_TIMESTAMP) AS TOTAL_YEARS FROM tbl_Company) SELECT cte_company.CompanyID, CompanyName, CompanyNumber, IncorporatedDate, TOTAL_YEARS, CASE WHEN TOTAL_YEARS > 1 THEN (SELECT DATEADD(MONTH, 9, MAX(TaxReturnDate)) FROM tbl_Tax WHERE cte_company.CompanyID = tbl_Tax.CompanyID) ELSE DATEADD(MONTH, 18, IncorporatedDate) END AS TaxDate FROM cte_company
Linq Query
IEnumerable<CompanyTaxInfo> result = from c in this.AcmeDB.tbl_Company let TotalYears = (DateTime.Now - c.IncorporatedDate).Value.Days / 365 let taxReturnDate = this.AcmeDB.tbl_Tax.Max(tx => tx.TaxReturnDate).Value.AddMonths(9) select new CompanyTaxInfo { CompanyID = c.CompanyID, CompanyName= c.CompanyName, CompanyNumber= c.CompanyNumber, IncorporatedDate= c.IncorporatedDate, TotalYears = TotalYears, TaxDate = TotalYears > 1? taxReturnDate: c.IncorporatedDate.Value.AddMonths(21) }; return result;
code is throwing DbArithmeticExpression arguments must have a numeric common type.' exception.
Please help
Try the following query:
var query = from c in this.AcmeDB.tbl_Company let TotalYears = EF.Functions.DateDiffYear(c.IncorporatedDate, DateTime.Now) select new CompanyTaxInfo { CompanyID = c.CompanyID, CompanyName = c.CompanyName, CompanyNumber = c.CompanyNumber, IncorporatedDate = c.IncorporatedDate, TotalYears = TotalYears, TaxDate = TotalYears > 1? this.AcmeDB.tbl_Tax.Where(tax => c.CompanyId == tax.CompanyId).Max(tx => tx.TaxReturnDate).Value.AddMonths(9): c.IncorporatedDate.Value.AddMonths(18) }; return query.ToList();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.