下面这个说法有什么问题
下面这个说法有什么问题吗?当在 LinqPad 中运行它并将语言设置为“C# 语句”时,我得到“;预期”。
from p in Products where p.UnitPrice > 50 select new {p.ProductID };
现在看起来如果我将它分配给任何 var;我没有收到任何错误。但令我感到困惑的是,下面的语句工作正常,并返回结果,尽管我没有将其分配给任何变量。有什么想法吗?
from p in Products
let spanishOrders = p.OrderDetails.Where ( o=> o.Order.ShipCountry == "Spain")
where spanishOrders.Any()
group new
{
p.ProductName,
Orders = spanishOrders.Count(),
spanishOrders
}
by p.Category.CategoryName
编辑:这是我的错,实际上我无法运行第二个示例而不将其分配给变量。
What is wrong with this statment below? I get "; expected" when run it in LinqPad with language setting to "C# Statement".
from p in Products where p.UnitPrice > 50 select new {p.ProductID };
Now seem like if I assign it to any var; I don't get any error. But what I find confusing is the statement below works fine and give me results back although I don't assign it to any variable. Any ideas?
from p in Products
let spanishOrders = p.OrderDetails.Where ( o=> o.Order.ShipCountry == "Spain")
where spanishOrders.Any()
group new
{
p.ProductName,
Orders = spanishOrders.Count(),
spanishOrders
}
by p.Category.CategoryName
EDIT: It was my bad actually i couldn't run the second example without assigning it to a variable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
LINQ 查询表达式不是 C# 中的合法语句。您需要在有效的语句中使用该表达式。
例如,您可以使用表达式作为赋值语句的右侧:
在我看来,您确实并不真正了解 LINQ 的含义。您期望您的裸查询表达式会做什么?
编辑:看看 Alex Moore 的答案,了解如何让它在 LINQPad 中工作。
顺便说一下,如果您仍然想坚持使用“C# 语句”模式,可以通过以下方法将查询结果写入控制台:
LINQ Query expressions are not legal statements in C#. You need to use the expression in a valid statement.
For example, you could use the expression as the right-hand side of an assignment statement:
It does appears to me like you don't really understand what LINQ is all about. What did you expect your naked query expression to do?
EDIT: Take a look at Alex Moore's answer for how to get this to work in LINQPad.
By the way, here's a way to get the results of the query written to console if you still want to stick with the "C# Statement(s)" mode:
如果您使用“C# 表达式”作为 LinqPad 语言下拉列表中的语言,则必须删除分号,因为它只是一个表达式:
如果您使用“C# 语句( s)”作为语言,您必须像常规 C# 代码一样编写它:
LINQPad 允许您测试两种方式,因为当您将语句移开时,某些查询会更容易。
If you are using "C# Expression" as the Language in the LinqPad language drop down, you have to drop the semi-colon because it's just an expression:
If you are using "C# Statement(s)" as the language, you will have to write it out like regular C# code:
LINQPad lets you test both ways, because some queries are easier when you get the statements out of the way.
尝试将结果分配给变量。
Try assigning the result to a variable.
您的代码不是有效的 C# 语句。
将表达式的值分配给变量:
或者删除末尾的分号并将语言更改为C# 表达式
Your code is not a valid C# statement.
Either assign the value of your expression to a variable:
Or remove the semicolon at the end and change the Language to C# Expression
您需要将其分配给某些东西。
You need to assign it to something.
对我来说唯一看起来奇怪的是,在新的匿名对象中,您没有将 ProductID 分配给属性,但我想这并不是真正需要的(我刚刚尝试过)。
编辑:由于评论,我意识到这不会为解决OP的问题提供任何有用的东西。
The only thing that looks weird to me is that in your new anonymous object, you aren't assigning the ProductID to a property, but I guess it's not really required (I just tried it).
EDIT: Due to the comments, I realize this will not provide anything useful for solving the the OP's problem.
您正在尝试在分配后进行选择。所以你应该使用这样的东西
you are trying to select after getting it assigned so . so you should use some thing like this