[英]SQL to LINQ Converter
I have a SQL and having a hard time to convert it to LINQ. 我有一个SQL,很难将其转换为LINQ。 Are there any other tools that can convert SQL to LINQ?
还有其他可以将SQL转换为LINQ的工具吗? I heard Linqer and tried it but it doesn't allow you to query on the Junction/Join Table of a Many-to-Many relationship tables.
我听到了Linqer的声音并尝试了它,但它不允许您在多对多关系表的连接/联接表上查询。 In any case here's the SQL that I need to convert to LINQ or even lambda.
无论如何,这是我需要转换为LINQ甚至lambda的SQL。
SELECT A.*
FROM
( SELECT TOP 10 T2.ID,
T2.Name,
SUM(T1.Column1 + T1.Column2 + T1.Column3) AS Total
FROM POS.dbo.Table1 T1
INNER JOIN POS.dbo.Table2 T2 on T2.ID = T1.ID
WHERE T2.ID IN
(
SELECT ID FROM POS.dbo.Table3 WHERE [Id] = 1
)
AND [Date] BETWEEN '2011-11-09 00:00:00' AND '2011-11-09 23:59:59'
GROUP BY T2.ID, T2.Name
ORDER BY Total DESC
) A
ORDER BY Name ASC
Here is my first attempt: 这是我的第一次尝试:
var query = db.Table1
.Include(e => e.Table2)
.Where(x => x.Date >= '2011-11-09 00:00:00'
&& x.DateCreated <= '2011-11-09 23:59:59')
.Where(y => y.Table2.Table3.Any(u => u.Id == 1))
.Take(10);
One of the things I like most about Linq is that it will combine queries together, so I break a complicated query into subqueries and let Linq recombine them all. 我最喜欢Linq的一件事是它将查询组合在一起,因此我将一个复杂的查询分解为子查询,然后让Linq重新组合所有查询。
Using that basis, does something like this work. 在此基础上,可以完成类似的工作。
DateTime startDate = new DateTime(2011,11,9);
DateTime endDate = new DateTime(2011,11,9,23,59,59);
var table3Ids = (from r in Pos.dbo.Table3 where id = 1 select r.id) ;
var query1 =
(
from t1 in Pos.dbo.Table1
where t1.Table2.Id == 1 && t1.Date >= startDate && t1.Date <= endDate
where table3Ids.Contains(t1.Table2.Id)
group t1 by new { t1.Table2.Id , t1.Table2.Name} into results
select new
{
results.Key.Id ,
results.Key.Name ,
Total = results.Sum(r=> (r.Column1 + r.Column2 + r.Column3))
}
);
var query2 = (from r in query1 orderby r.Total descending select r).Take(10);
var query3 = (from r in query2 orderby r.Name select r);
var list = query3.ToList();
Old thread, but relevant: SQL to LINQ Tool 旧线程,但相关: SQL to LINQ工具
I did some digging / asked a couple of co-workers and it looks like Linqer is still the only tool available for this (at least the only one well known enough to come up in conversation) 我进行了一些挖掘/询问了几个同事,看来Linqer仍然是唯一可用于此目的的工具(至少是唯一一个众所周知的对话工具)。
Here's my shot - it's a bit hard without knowing the actual data structure or the expected results: 这是我的镜头-在不知道实际数据结构或预期结果的情况下有点困难:
var query = db.Table1
.Join(db.Table2, table1 => table1.Id, table2 => table2.Id, (t1, t2) => new {
Id = t2.Id,
Name = t2.Name,
Total = (t1.Column1 + t1.Column2 + t1.Column3),
Date = t1.Date //you didn't use a table alias for this field, so I'm not sure which table it comes from
})
.Where(x => x.Date >= new DateTime(2011, 11, 9)
&& x.DateCreated <= new DateTime(2011, 11, 9, 23, 59, 59))
.Join(db.Table3, x => x.Id, table3 => table3.Id, (x, t3) => new {
Id = x.Id,
Name = x.Name,
x.Total
})
.Where(x => x.Id == 1)
.OrderByDescending(x => x.Total)
.ThenBy(x => x.Name)
.Take(10)
.ToList()
Because that might be the craziest Linq statement I've ever written, don't assume it'll work the first time, but it's at least a good starting point. 因为那可能是我写过的最疯狂的Linq语句,所以不要以为它会在第一时间生效,但这至少是一个很好的起点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.