LINQ - 使用 Select - 理解 select

发布于 2024-11-09 02:54:44 字数 578 浏览 0 评论 0原文

我发现 LINQ 有点难以理解。我喜欢这个概念并相信它有很大的潜力。但是,写了这么多 SQL 后,语法对我来说并不容易接受。

A.多种选择方式有什么关系?

我发现我能够创建上下文并使用方法执行 Select() 。

context.Table.Select(lamba expression);

好吧...我为什么要使用这个?它与这种类型的选择相比(或者确实如此)如何?

var returnVal = from o in context.Table
                orderby o.Column
                select o;

B.请解释一下变量的性质

**来自 context.Table 中的 X**

为什么我们在这里贴一个看似任意命名的变量?这不应该是 类型的已知类型吗?

I find LINQ a little difficult to wrap my head around. I like the concept and believe it has lots of potential. But, after writing so much SQL the syntax is just not easy for me to swallow.

A. What is the deal with multiple ways to select?

I see that I am able to create a context and perform a Select() using a method.

context.Table.Select(lamba expression);

ok...Why would I use this? How does it compare to (or does it) this type of select?

var returnVal = from o in context.Table
                orderby o.Column
                select o;

B. Please explain the variable nature of

**from X** in context.Table

Why do we stick a seemingly arbitrarily named variable here? Shouldn't this be a known type of type <Table>?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

悲念泪 2024-11-16 02:54:44

所以...

var returnVal = context.Table.Select(o => o);

var returnVal = from o in context.Table
                select o;

是一样的。在第二种情况下,C# 只是提供了很好的语法糖来为您提供更接近普通 SQL 语法的内容。请注意,我从您的第二个查询中删除了 orderby。如果你想要在那里,那么第一个将变成:

var returnVal = context.Table.OrderBy(o => o.Column).Select(o => o);

至于你的最后一个问题......我们不会在这里粘贴任意命名的变量。我们为每一行指定一个名称,以便稍后在语句中引用它。它是隐式类型的,因为系统知道 Table 包含什么类型。

为了回应您的评论,我想补充一点想法。您提到正常的方法调用会变得很糟糕。确实可以。下面是一个简单的示例,其中 LINQ 语法立即变得更加清晰(至少,如果您习惯 SQL 语法)

var returnVal = context.Table.OrderBy(o => o.Column1)
                             .ThenBy(o => o.Column2)
                             .ThenBy(o => o.Column3)
                             .Select(o => o);

var returnVal = from o in context.Table
                orderby o.Column1, o.Column2, o.Column3
                select o;

So...

var returnVal = context.Table.Select(o => o);

and

var returnVal = from o in context.Table
                select o;

are the same. In the second case, C# just has nice syntactic sugar to give you something closer to normal SQL syntax. Notice I removed the orderby from your second query. If you wanted that in there, then the first one would become:

var returnVal = context.Table.OrderBy(o => o.Column).Select(o => o);

As for your last question... we're not sticking an arbitrarily named variable here. We're giving a name to each row so that we can reference it later on in the statement. It is implicitly typed because the system knows what type Table contains.

In response to your comment, I wanted to add one more thought. You mentioned things getting nasty with the normal method calls. It really can. Here's a simple example where its immediately much cleaner (at least, if you're used to SQL syntax) in the LINQ syntax:

var returnVal = context.Table.OrderBy(o => o.Column1)
                             .ThenBy(o => o.Column2)
                             .ThenBy(o => o.Column3)
                             .Select(o => o);

versus

var returnVal = from o in context.Table
                orderby o.Column1, o.Column2, o.Column3
                select o;
云裳 2024-11-16 02:54:44

答:这个是一样的。编译器将查询表达式转换为方法调用。完全一样。

B: x 与 foreach(var X in context.Table) 中的 x 相同。您可以为表/序列的单个元素定义名称。

A: this is the same. The compiler transforms the query expression to method calls. Exactly the same.

B: The x is the same as in foreach(var X in context.Table). You define a name for an individual element of the table/sequence.

酒绊 2024-11-16 02:54:44

在 B 中,X 的类型是隐式的。你可以很容易地做类似的事情:

from Row x in context.Table

而且结果是一样的。在 A 中,使用 lambda 和等效的完整 LINQ 语法没有任何区别,只是您永远不会执行 .Select(x => x)。是用来改造物品的。假设您有一个整数列表,.Select(x => x * x) 将返回每个整数的平方。

In B, X's type is implicit. You could just as easily do something like:

from Row x in context.Table

and it would be the same. In A, there isn't any difference between using a lambda and the equivalent full-LINQ syntax, except that you would never do .Select(x => x). It's for transforming items. Say you had a list of integers, .Select(x => x * x) would return the square of each of them.

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