如何将此SQL查询转换为Linq? [英] How Do I Convert This Sql Query Into Linq?

查看:70
本文介绍了如何将此SQL查询转换为Linq?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在学习将LINQ转换为所有SQL查询。但我会转换这个复杂的SQL我该怎么办?我到达了一点,我不知道怎么处理这个小组...你能帮我一把吗?



这是SQL查询ORIGINAL:



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 :

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





我的LINQ就是这个,但我被困住了:





My LINQ is this but I'm stuck :

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!! */





怎么做?



我等待新闻。



谢谢



Cris



How is it done?

I await news .

Thank you

Cris

推荐答案

这是否能满足您的需求:



Does this do what you need:

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();







由于您已经对select中的所有列使用了group by,因此无需执行不同的操作。




There is no need to perform a distinct since you have already used group by for all the columns in select.


使用工具linqpad [ ^ ]这是免费的。

你也可以使用Linqer这是一个SQL到LINQ转换工具。它有助于学习LINQ并转换现有的SQL语句。
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.


这篇关于如何将此SQL查询转换为Linq?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆