Covert SQL to Linq Query - sql

I am doing my final year's project, I am new to linq as well. I have an SQL statement which i want to convert it to linq. I am currently using .net core.
I will greatly appreciate any help ,
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, 21, IncorporatedDate)
END AS TaxDate
FROM cte_company
I tried :
var result = (from comp in this.AccountDB.TblCompanies
where comp.CompanyStatus == true && comp.UserName == username
join tax in this.AccountDB.TblTaxes
on comp.CompanyId equals tax.CompanyId
orderby tax.TaxReturnDate descending
select new CompanyTaxInfo
{
CompanyName = comp.CompanyName,
CompanyID = comp.CompanyId,
CompanyNumber = comp.CompanyNumber,
})
.ToList();

As far as I can tell, the value read from tbl_tax is constant, which means it can be reduced to this:
var taxReturnDate = tbl_tax.Max(tx=>tx.TaxReturnDate).AddMonths(9);
var result = from c in tbl_Company
let TotalYears = (DateTime.Now - c.IncorporatedDate).Days / 365
select new
{
CompanyID = c.CompanyID,
c.CompanyName,
c.CompanyNumber,
c.IncorporatedDate,
TotalYears,
TaxDate = TotalYears > 1 ? taxReturnDate : c.IncorporatedDate.AddMonth(9)
}

You could still use your SQL with EF and still get the same result
for example if you create a stored procedure for your SQL
you could call it as the following
var result = await dbContext.<Your-Result-set>.FromSqlInterpolated(#$"[dbo].[Your-Procedure-Name]
{your-parameter1}
,{your-parameterN}
").ToListAsync();

Related

How do I translate my SQL Query with Having MAX in LINQ?

I'd like to translate this SQL Query in LINQ with EF
SELECT Agts.AgtNum, Agts.AgtLastname, Agts.AgtFirstname, COUNT(Co.CoEnd) FROM [dbo].Agts AS Agts
INNER JOIN [dbo].[Contracts] AS Co ON Agts.AgtNum = Co.AgtNum
GROUP BY Agts.AgtNum, Agts.AgtLastname, Agts.Firstname
HAVING MAX(Co.CoEnd) <= '2020-05-17'
ORDER BY AgtNum asc
I tried that :
public List<AgentToPurge> AgentsToPurge(DateTime datePurge)
{
return (from agent in this.Entities.Agts
join contract in this.Entities.Contracts on agent.AgtNum equals contract.AgtNum
group agent by agent.AgtNum into g
where g.CoEnd <= datePurge
select new AgentToPurge
{
Id = g.Key,
Lastname = g.Key.AgtLastname,
Firstname = g.Key.AgtFirstname,
Contract_Deleted = g.Key.CoEnd.Count()
}).ToList();
}
But the line
where g.CoFin <= datePurge
doesn't work.
I think my "select new" isn't correct either.
Could you help me to solve this ?
Try the following query:
public List<AgentToPurge> AgentsToPurge(DateTime datePurge)
{
return (from agent in this.Entities.Agts
join contract in this.Entities.Contracts on agent.AgtNum equals contract.AgtNum
group contract by new { agent.AgtNum, agent.AgtLastname, agent.AgtFirstname } into g
where g.Max(x => x.CoEnd) <= datePurge
select new AgentToPurge
{
Id = g.Key.AgtNum,
Lastname = g.Key.AgtLastname,
Firstname = g.Key.AgtFirstname,
Contract_Deleted = g.Sum(x => x.CoEnd != null ? 1 : 0)
}).ToList();
}
Note that LINQ query is built from classes and navigation properties and probably you will not need JOIN, if you have properly defined Model.

I need to transform a SQL query with inner joins to Linq in Entity Framework

I am very new to translating queries to entity, I don't know how to replace that query into linq in my code
select
brules.rule_description, brules.user_story_number,
so.source_name,
count(dlog.row_id) as error_count,
cast(execution_date as date) as execution_date
from
br.lk_business_rules brules
inner join
br.business_rules_detailed_log dlog on dlog.user_story_number = brules.user_story_number
inner join
br.lk_business_rules_source so on so.source_id = source_id_fk
where
brules.status_id_fk = 3
group by
brules.rule_description, brules.user_story_number,
so.source_name, cast(execution_date as date)
order by
brules.rule_description
i tried this:
var query = from br in _context.Lk_business_rules
join detLog in _context.Business_rules_detailed_log
on br.User_story_number equals detLog.User_story_number
join source in _context.Lk_business_rules_source
on detLog.Source_id equals source.Source_id
where br.Status_id_fk == 3
select new
{
Business_rule_description = br.Rule_description,
Business_rule_storynumber = br.User_story_number,
Source = source.Source_name,
Error_count = detLog.Row_id.Count,
Date = detLog.Execution_date
};
but not succed

Convert SQL to Linq select in where

I have three table below:
TABLE_PRODUCT (IdProduct, ProductName, ProductUnit)
TABLE_STORE_HOUSE (IdContain, IdProduct, ProductNumber, TimeInput)
TABLE_SELL (IdSell, IdContain, ProductNumberSell, TimeSell)
Current, How to using LinQ query get TABLE_STORE_HOUSE.IdProduct witch condition TABLE_STORE_HOUSE.ProductNumber - Sum(TABLE_SELL.ProductNumberSell) > 0 and TABLE_STORE_HOUSE.TimeInput is smallest
Help me convert Sql to Linq..............
select top 1 IdContain
from
TABLE_STORE_HOUSE
where IdProduct = '6'
and
ProductNumber - (select sum(ProductNumber)
from TABLE_SELL
Where TABLE_SELL.IdContain = IdContain)> 0
order by TimeInput desc;
Can you try this?
from t in TABLE_STORE_HOUSEs
let TSell = (
from s in TABLE_SELLs
where s.IdContain == t.IdContain
orderby s.ProductNumber
select new {
s.ProductNumber
}
)
where t.IdProduct == 6 && (t.ProductNumber - TSell.Sum(si => si.ProductNumber)) > 0
select new { t.IdContain }
for top 1 you can use Take() function.

Linq union all equivalent of sql code

I have the following queries:
var majorClients = maj in dbContext.MajorClients
where (maj.startdate > startDate)
where (maj.status == "Active")
Select new Client{EntityPK = maj.mjPrimaryKey,Name = maj.name, Type = "Maj"};
var minorClients = min in dbContext.MinorClients
where (min.startdate > startDate)
where (min.status == "Active" || min.status== "Inactive")
Select new Client{EntityPK = min.mnPrimaryKey,Name = min.name, Type = "Min"};
There are clients that could appear in both major and minor tables. I would like to return a list of all occurrences of clients in both tables, however if there are matching clients by name, then I would only want to return the matching record from the majorClients table.
I have written a sql query to return the results:
SELECT mjPrimaryKey AS EntityPK,name,'Maj' AS TYPE
FROM majorClients
WHERE status = 'Active' AND startDate > #startDate
UNION ALL
SELECT mnPrimaryKey,name,'Min' FROM minorClients
WHERE status IN ('Active','Inactive') AND startDate > #startDate
WHERE name NOT IN (SELECT name FROM majorClients WHERE status = 'Active' AND startDate > #startDate)
How would I represent this query in linq?
Try this linq. To exclude duplicates from minorClients, I've used Contains method. To union all objects - Union method:
var majorClients = from maj in dbContext.MajorClients
where maj.startdate > startDate
&& maj.status == "Active"
select new Client
{
EntityPK = maj.mjPrimaryKey,
Name = maj.name,
Type = "Maj"
};
var minorClients = from min in dbContext.MinorClients
where min.startdate > startDate
&& min.status == "Active" || min.status== "Inactive"
&& !(from maj in dbContext.MajorClients
where maj.startdate > startDate
&& maj.status == "Active"
select maj.name).Contains(min.Name)
select new Client
{
EntityPK = min.mnPrimaryKey,
Name = min.name,
Type = "Min"
};
var allClients = majorClients.Union(minorClients);

linq group by multipe aggregates

Im new to Linq and Im sure that I have gone about this in a convoluted manner. Im trying to do something like this SQL in Linq:
SELECT DISTINCT
count(vendor) as vendorCount,
reqDate,
status,
openDate,
item,
poDate,
count(responseDate) as responseCount
FROM
myTable
GROUP BY
reqDate, status, openDate, item, poDate
HAVING
reqDate > openDate
Here is what I have so far.
var groupQuery = (from table in dt.AsEnumerable()
group table by new
{
vendor = table["vendor"], reqdate = table.Field<DateTime>("ReqDate"), status = table["status"],
open = table["openDate"],
item = table["item"),
podate = table.Field<DateTime>("PODate"), responsedate = table.Field<DateTime>("responseDate"),
}
into groupedTable
where Having(groupedTable.Key.reqdate, groupedTable.Key.openDate) == 1
select new
{
x = groupedTable.Key,
y = groupedTable.Count()
}).Distinct();
foreach (var req in groupQuery)
{
Console.WriteLine("cols: {0} count: {1} ",
req.x, req.y);
}
The Having() is a function that takes two datetime parameters and returns a 1 if the reqDate is greater than the openDate. It compiles and runs, but it obviously does not give me the results I want. Is this possible using Linq? I want to push this data to an excel spreadsheet so Im hoping to create a datatable from this linq query. Would I be better off just creating a dataview from my datatable and not mess with Linq?
The SQL code is grouping by only some of the fields, while your LINQ statement is grouping by all of the fields, so the only items that would get grouped would be duplicates. If you group by only the fields that the SQL query groups by, you should get the correct answer. Your Having() method words fine, but is not necessary and is less readable.
var groupQuery = (from table in dt.AsEnumerable()
group table by new
{
reqdate = table.Field<DateTime>("ReqDate"),
status = table["status"],
open = table["openDate"],
item = table["item"),
podate = table.Field<DateTime>("PODate")
}
into groupedTable
where groupedTable.Key.reqdate > groupedTable.Key.openDate
select new
{
x = groupedTable.Key,
VenderCount = groupedTable.Select(t => t["vendor"])
.Distinct()
.Count(),
ResponseCount = groupedTable.Select(t => t.Field<DateTime>("responseDate"))
.Distinct()
.Count()
}).Distinct();