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.
Related
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,
};
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 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
I have a SQL query and I am trying to convert it to LINQ. But when I do so, I am getting this error:
SQL cannot be converted to LINQ: DISTINCT an Agregate count cannot be converted to LINQ
SELECT
ST_CN_NO,
DN_DT,
count(ST_CN_NO) as totlines,
count(distinct LOC_CODE) as totloc,
sum(SN_QTY)as totscnqty,
sum(SAP_QTY) as totmpqty
from physical_count
where 1=1
group by ST_CN_NO,DN_DT
order by physical_count.ST_CN_NO
So, how can I convert count(distinct LOC_CODE) to LINQ? Is there any way to do so?
This is my LINQ query whithout adding count(distinct LOC_CODE):
from t in db.PHYSICAL_COUNT
where
1 == 1
group t by new {
t.ST_CN_NO,
t.DN_DT
} into g
orderby
g.Key.ST_CN_NO
select new {
ST_CN_NO = (Int32?)g.Key.ST_CN_NO,
g.Key.DN_DT,
totlines = (Int64?)g.Count(),
totscnqty = (Int32?)g.Sum(p => p.SN_QTY),
totmpqty = (Int32?)g.Sum(p => p.SAP_QTY)
}
I have a problem with one of my queries using linq 2 entites. I'm trying to view the query before it is generated to the db but without any success. I tried using ToTraceString() but couldn't cause the query could not be cast to ObjectQuery.
this is my query:
var movies = (from m in watchedRepo.GetAll().Where(c => c.iUserId == userId).ToList()
join f in moviePageViewsRepository.GetAll() on m.iMovieId equals f.iMovieId
group f by new JoinClass { MovieId = f.iMovieId, Points = m.iPoints }
into g
orderby g.Key.Points descending , g.Sum(d => d.iScore) descending
select new JoinClass { MovieId = g.Key.MovieId, Points = g.Key.Points, PageViews = g.Sum(d => d.iScore) }).Skip(skip).Take(take);
if I try to execute it from linq I get an out of memory exception.
any ideas please?
The problem is with your ToList() in the first line of your query. This will immediately fetch all rows from wachtedRepo with the specified UserId.
Because your query will then become a hybrid Linq to Entities / Linq to Objects, you can't cast to an ObjectQuery. Is your query still working when you remove ToList()?
If you want to see what's really happening in your Sql Server, I would suggest using the Sql Server Profiler tools to view your queries.
Assuming watchedRepo is the Linq to SQL object representing the database connection
watchedRepo.Log = Console.Error;
All SQL queries will be printed to standard error.