将 SQL 查询转换为 Linq 查询?
我有一个 SQL 查询,我想转换为 LINQ 查询,请帮助我!
SELECT TOP 10 UPER( C1.NAME)
FROM CUSTOMER C1 WHERE C1.NAME LIKE 'A%' AND C1.ID NOT IN
(SELECT TOP 20 C2.ID FROM CUSTOMER C1 WHERE C1.NAME LIKE 'A%' ORDER BY C2.NAME)
ORDER BY C1.NAME
更新
SELECT TOP 10 UPPER( C1.NAME)
FROM CUSTOMER C1 WHERE C1.NAME LIKE 'A%' AND C1.ID NOT IN
(SELECT TOP 20 C2.ID FROM CUSTOMER C2 WHERE C2.NAME LIKE 'A%' ORDER BY C2.NAME)
ORDER BY C1.NAME
Datacontext 类声明为“db”。 抱歉我的英语不好!
I have a SQL query and i want to convert to LINQ quyery, plz help me !
SELECT TOP 10 UPER( C1.NAME)
FROM CUSTOMER C1 WHERE C1.NAME LIKE 'A%' AND C1.ID NOT IN
(SELECT TOP 20 C2.ID FROM CUSTOMER C1 WHERE C1.NAME LIKE 'A%' ORDER BY C2.NAME)
ORDER BY C1.NAME
UPDATE
SELECT TOP 10 UPPER( C1.NAME)
FROM CUSTOMER C1 WHERE C1.NAME LIKE 'A%' AND C1.ID NOT IN
(SELECT TOP 20 C2.ID FROM CUSTOMER C2 WHERE C2.NAME LIKE 'A%' ORDER BY C2.NAME)
ORDER BY C1.NAME
The Datacontext class declared "db".
Sorry about my poor english !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对我来说,这看起来并不太复杂,除非我读错了:
很多人似乎将 SQL 直接转换为 LINQ,但看起来他们忽略了复合查询的要点(事实上您正在使用相同的 ORDER-BY 从同一个表中进行选择)。
Skip
方法消除了对C1.ID NOT IN (SELECT TOP 20...
部分的需要,因此您可以在一个 LINQ 查询中完成这一切。一个小注释:如果您的数据库不区分大小写(很可能是这样),您可能需要将
where c.name.StartsWith("A")
替换为where c.name.StartsWith("A", StringComparison.OrdinalIgnoreCase)
不过,我不确定;我会先尝试不使用StringComparison
。To me that doesn't look too complex, unless I'm reading it wrong:
A lot of people seem to be translating your SQL directly into LINQ, but it looks like they're missing the point of your compound query (and the fact that you're selecting from the same table with the same ORDER-BY). The
Skip
method removes the need for theC1.ID NOT IN (SELECT TOP 20...
part, so you can do it all in one LINQ query.One tiny note: If your database is case-insensitive (which it probably is), you might need to replace
where c.name.StartsWith("A")
withwhere c.name.StartsWith("A", StringComparison. OrdinalIgnoreCase)
. I'm not sure about that, though; I'd try it without theStringComparison
first.我想你已经得到了答案,所以我想向你介绍一个有用的工具
使您能够将 SQL 查询转换为 LINQ 查询。
它的 LINQPad :
I think you got your answer, so I want to introduce a useful tools for you that
enable you to converting SQL queries to LINQ queries.
Its LINQPad :
我认为有更好的方法可以做到这一点,但这就是我的全部努力......
:D
I think there is more better way of doing this but this was all my effort...
:D