Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello to all,

I am learning to convert LINQ all the SQL queries . But would I convert this complex SQL how can I? I arrived at one point and I do not know how to do with the group by having ... could you please give me a hand ?

Here is the SQL query ORIGINAL :

SQL
SELECT DISTINCT MIN(nomeFileVeicolo) AS nomeFileVeicolo, porte, body, nomeBody, numModello, modelloDa,modelloA,specie 
FROM DataBaseCar 
GROUP BY Class_ID,idMarca, nomeModello, porte, body, nomeBody, numModello, modelloDa,modelloA,specie 
HAVING (Class_ID = 1) AND (idMarca = '6') AND (nomeModello = '2-Serie') AND (specie = 'CAR') 
ORDER BY modelloDa DESC


My LINQ is this but I'm stuck :

C#
var veicoli = from db in contestoDB.DataBaseCar
                                  group db by new {db.Class_ID,  db.idMarca,  db.nomeModello,  db.porte,  db.body,  db.nomeBody,  db.numeroModello,  db.modelloDa,  db.modelloA,  db.specie} into g
                                  where g.Single().Class_ID == 1
                                  where g.Single().idmarca== marca
                                  where g.Single().nomeModello== modello
                                  where g.Single().specie== tipo
                                  orderby db.

/* marca , tipo, modello input parameters!! */


How is it done?

I await news .

Thank you

Cris
Posted

Does this do what you need:

C#
contestoDB.DataBaseCar.Where(x =>    x.Class_ID == 1 &&
                                     x.idMarca == "6" &&
                                     x.nomeModello == "2-Serie" &&
                                     x.specie == "CAR") 
                     .GroupBy(x => new { x.Class_ID, 
                                         x.idMarca, 
                                         x.numModello, 
                                         x.porte, 
                                         x.body, 
                                         x.nomeBody, 
                                         x.modelloDa, 
                                         x.modelloA, 
                                         x.specie }) 
                     .Select(x => x.OrderBy(y => y.nomeFileVeicolo))
                     .Select(x => x.First())
                     .OrderByDescending(x => x.modelloDa).ToList();



There is no need to perform a distinct since you have already used group by for all the columns in select.
 
Share this answer
 
v6
Comments
Cristian Capannini 13-Jan-16 4:46am    
I'll try it now.... thanks d@nish
Cristian Capannini 13-Jan-16 4:50am    
I've tried just now but i got exception :
{ " You can not sort ' System.Linq.IOrderedEnumerable`1 [ DataBase.DataBaseCar ] ' to type . " }
Why?
PS : This exception in italian is : {"Impossibile ordinare 'System.Linq.IOrderedEnumerable`1[DataBase.DataBaseCar]' per tipo."}
dan!sh 13-Jan-16 4:54am    
Oh yes, the OrderBy should be a select. Updated the reply.
use tool linqpad[^] which is free.
you can also use Linqer which is a SQL to LINQ conversion tool. It helps learning LINQ and convert existing SQL statements.
 
Share this answer
 
Comments
Cristian Capannini 13-Jan-16 4:54am    
Tried linqpad not convert SQL to LINQ. And Linqer trial not working!!! Time is always expired!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900