How can I convert this sql statement to linq expression - c#

How can I convert this sql query to linq expression, I'm new in linq and searched this but I could not understand how to convert it.
SELECT
a.afiliacaoid,
DATE_FORMAT(a.horario, '%d/%m/%Y') data,
COUNT(a.afiliacaoid) acessos,
IFNULL(p.pedidos,0) pedidos
FROM
acesso a LEFT JOIN
(SELECT p.afiliacaoid,
DATE_FORMAT(p.cadastro , '%d/%m/%Y') data,
COUNT(p.afiliacaoid) pedidos
FROM pedido p) p ON a.afiliacaoid = p.afiliacaoid
AND DATE_FORMAT(a.horario, '%d/%m/%Y') = data
WHERE
a.afiliacaoid=1
GROUP BY DATE_FORMAT(a.horario, '%d/%m/%Y')

I would suggest using this tool to convert: https://www.linqpad.net/

This is a good tool that might be helpful to you - http://www.sqltolinq.com

Related

Using 'OR' inside a LinQ join query (adapting SQL into LinQ)

I have a SQL query that basically joins the MyWords table to the MyTranslations table on two keys. Here are my tables for better understanding.
MyWords table:
Id
WordString
LangType
MyTranslations table:
Id
WordColumnAId
WordColumnBId
Please note that WordColumnAIdand WordColumnBId columns are FKs that represents the MyWords.Id.
My SQL query:
SELECT MyTranslations.Id
,WordColumnAId
,WordColumnBId
,MyWords.WordString
,MyWords.LangType
FROM MyTranslations
join MyWords on (MyTranslations.WordColumnAId = MyWords.Id or MyTranslations.WordColumnBId = MyWords.Id)
where MyWords.LangType !=#currentLangType
I know it doesn't make sense to use join on with multiple keys at first glance but I'm trying to a cross-query that would join two tables whether the key is on WordColumnAId or WordColumnBId.
Problem
I'm trying to adapt the above T-SQL query into a LinQ query. The problem is that I can't find my way around LinQ to use two keys in a single join query.
Here's what I've got so far:
from translation in queryableTranslation
join word in _myWordRepository on translation.WordColumnAId equals word.Id // This is where I want to add `or translation.WordColumnBId equals word.Id` but get errors.
where word.LangType != currentLangType
select new QueryResultDto {
MyTranslationId = translation.Id,
WordString = word.WordString,
LanguageType = word.LangType,
WordColumnAId = translation.WordColumnAId,
WordColumnbId=translation.WordColumnbId,
};
I'm very new to the LinQ and trying to learn.
So my question: is there a way to achieve this in LinQ or am I trying the impossible? I'm also open to better approaches.
EF and other LINQ providers should translate query to INNER JOIN when using this syntax:
var query =
from translation in queryableTranslation
from word in _myWordRepository.Where(word => translation.WordColumnAId == word.Id
|| translation.WordColumnBId = word.Id)
where word.LangType != currentLangType
select new QueryResultDto
{
MyTranslationId = translation.Id,
WordString = word.WordString,
LanguageType = word.LangType,
WordColumnAId = translation.WordColumnAId,
WordColumnbId=translation.WordColumnbId,
};

Implementation of [PARTITION BY] command by LINQ

I did my best search to convert PARTION BY command from TSQL to LINQ command. But apparently there is no way to convert it.
This is my code to which I am going to convert:
WITH MyRowSet
AS
(
SELECT OrderDate
,SalesOrderNumber
,AccountNumber
,CustomerID
,ROW_NUMBER() OVER (PARTITION BY CustomerID ORDER BY CustomerID, OrderDate DESC) AS RowNum
FROM [Sales].[SalesOrderHeader]
)
SELECT * FROM MyRowSet WHERE RowNum = 1
If exist any solution I would be greatful to know.
linq2db has this feature among with CTE. If you already work with EF Core, you can extend your LINQ queries by extension linq2db.EntityFrameworkCore
This SQL can be written by LINQ
var rnQuery =
from oh in db.SalesOrderHeader
select new
{
oh.OrderDate,
oh.SalesOrderNumber,
oh.AccountNumber,
oh.CustomerID,
RowNum = Sql.Ext.RowNumber().Over().PartitionBy(oh.CustomerID)
.OrderByDesc(oh.OrderDate).ToValue()
};
// switch to alternative LINQ Translator
rnQuery = rnQuery.ToLinqToDB();
var query =
from q in rnQuery.AsCte("MyRowSet")
where q.RowNum == 1
select q;
I have simplified your OrderBy - CustomerID is not needed if you are making partition by this field.

How convert linq query to SQL query?

I want convert following LINQ query to SQL query.
var ACTIVITY_ROYALITY_MONTH = from m in db.MiningPermit
join pm in db.Permit_Mineral on m.MINING_PERMIT_ID equals pm.MINING_PERMIT_ID
join r in db.Royality on pm.Permit_Minerals_ID equals r.Permit_Minerals_ID
where r.ORDER_ID == 0 // NULL in server
orderby r.YEAR, r.MONTH
group r by new { m.MINING_PERMIT_ID , r.YEAR, r.MONTH } into mpmr
select mpmr.ToList();
Use Linqpad and recreate the linq (even by bringing in your assemblies) in a C# query. Run the query. Then in the output, there is a selection button of SQL which will show the sql code.

How to convert PostgreSQL query to Linq to SQL for NHibernate

I am using NHibernate and LINQ to SQL and I want to convert the following SQL query:
select min(T."CustomerName") from public."Jobs" as T group by lower(T."CustomerName");
I want to convert it in LINQ to SQL and add it in a DAO.
Please help.
At last I have found the answer.
Here it is:
return HibernateTemplate.Execute(session => (from r in session.Query<Job>()
group r by r.CustomerName.ToLower()
into g
let c = g.Min(l => l.CustomerName)
orderby c
select c)).ToList();
Thank you people.

How to translate SQL statement with multiple join conditions based on subquery to LINQ

This might be one of those situations where plain SQL commands are better than LINQ. Here's a simplified version of the SQL statement I'm trying to translate:
SELECT * FROM IDTable AS idt
INNER JOIN NameTable AS nt ON nt.IDTableID=idt.Id
AND nt.Id= (SELECT TOP(1) Id
FROM NameTable AS nt2
WHERE nt2.IDTableID=11 ORDER BY nt2.DateInserted DESC)
I have the LINQ query to pull records when just joining on IDs and I've seen how to join on multiple columns, but I have no idea how to plug the subquery into the mix.
If this isnt entirely clear, please let me know and I'll edit to elaborate.
Maybe something like this?
var results = from id in db.IDTable
join n in db.NameTable on id.Id equals n.IDTableID
where n.Id = (
from n2 in db.NameTable
where n2.IDTableID = 11
orderby n2.DateInserted desc
).First()
select new { id, n };

Categories

Resources