将 SQL 查询转换为 Linq 查询?

发布于 2024-11-05 18:19:42 字数 533 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

日暮斜阳 2024-11-12 18:19:42

对我来说,这看起来并不太复杂,除非我读错了:

var query = from c in db.Customer
            where c.name.StartsWith("A")
            orderby c.name
            select c.name.ToUpper();

return query.Skip(20).Take(10).ToList();

很多人似乎将 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:

var query = from c in db.Customer
            where c.name.StartsWith("A")
            orderby c.name
            select c.name.ToUpper();

return query.Skip(20).Take(10).ToList();

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 the C1.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") with where c.name.StartsWith("A", StringComparison. OrdinalIgnoreCase). I'm not sure about that, though; I'd try it without the StringComparison first.

腹黑女流氓 2024-11-12 18:19:42

我想你已经得到了答案,所以我想向你介绍一个有用的工具
使您能够将 SQL 查询转换为 LINQ 查询。
它的 LINQPad

好吧,你不必这样做! LINQPad 让
您以交互方式查询数据库
现代查询语言:LINQ。吻
再见 SQL Management Studio!

LINQPad 支持 C# 4.0 中的所有内容
和框架4.0:

LINQ 到对象
LINQ to SQL 和实体框架
LINQ 到 XML
并行LINQ

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 :

Well, you don't have to! LINQPad lets
you interactively query databases in a
modern query language: LINQ. Kiss
goodbye to SQL Management Studio!

LINQPad supports everything in C# 4.0
and Framework 4.0:

LINQ to Objects
LINQ to SQL and Entity Framework
LINQ to XML
Parallel LINQ
污味仙女 2024-11-12 18:19:42
var result = (from o in db.Customer
             where SqlMethods.Like(o.Name, "A%")
             order by i.Name
             select o.Name.ToUpper()).Skip(20).Take(10);

我认为有更好的方法可以做到这一点,但这就是我的全部努力......

:D

var result = (from o in db.Customer
             where SqlMethods.Like(o.Name, "A%")
             order by i.Name
             select o.Name.ToUpper()).Skip(20).Take(10);

I think there is more better way of doing this but this was all my effort...

:D

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文