下面这个说法有什么问题

发布于 2024-12-13 20:33:26 字数 575 浏览 0 评论 0原文

下面这个说法有什么问题吗?当在 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 技术交流群。

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

发布评论

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

评论(8

花桑 2024-12-20 20:33:26

LINQ 查询表达式不是 C# 中的合法语句。您需要在有效的语句中使用该表达式。

例如,您可以使用表达式作为赋值语句的右侧:

var expensiveProductIds = from p in Products
                          where p.UnitPrice > 50 
                          select new { p.ProductID };

在我看来,您确实并不真正了解 LINQ 的含义。您期望您的裸查询表达式会做什么?

编辑:看看 Alex Moore 的答案,了解如何让它在 LINQPad 中工作。

顺便说一下,如果您仍然想坚持使用“C# 语句”模式,可以通过以下方法将查询结果写入控制台:

var expensiveProductIds = from p in Products
                          where p.UnitPrice > 50 
                          select new { p.ProductID };

expensiveProductIds.Dump();

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:

var expensiveProductIds = from p in Products
                          where p.UnitPrice > 50 
                          select new { p.ProductID };

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:

var expensiveProductIds = from p in Products
                          where p.UnitPrice > 50 
                          select new { p.ProductID };

expensiveProductIds.Dump();
凤舞天涯 2024-12-20 20:33:26

如果您使用“C# 表达式”作为 LinqPad 语言下拉列表中的语言,则必须删除分号,因为它只是一个表达式:

from p in Products where p.UnitPrice > 50 select new {p.ProductID }

如果您使用“C# 语句( s)”作为语言,您必须像常规 C# 代码一样编写它:

var x = (from p in Products where p.UnitPrice > 50 select new {p.ProductID });

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:

from p in Products where p.UnitPrice > 50 select new {p.ProductID }

If you are using "C# Statement(s)" as the language, you will have to write it out like regular C# code:

var x = (from p in Products where p.UnitPrice > 50 select new {p.ProductID });

LINQPad lets you test both ways, because some queries are easier when you get the statements out of the way.

粉红×色少女 2024-12-20 20:33:26
            var db = new DataContext();
            var product= from p in db.Products 
                          where p.UnitPrice > 50
                          select new{ProductId = p.ProductID };
            var db = new DataContext();
            var product= from p in db.Products 
                          where p.UnitPrice > 50
                          select new{ProductId = p.ProductID };
小傻瓜 2024-12-20 20:33:26

尝试将结果分配给变量。

var v = from p in Products where p.UnitPrice > 50 select new {p.ProductID }; 

Try assigning the result to a variable.

var v = from p in Products where p.UnitPrice > 50 select new {p.ProductID }; 
青柠芒果 2024-12-20 20:33:26

您的代码不是有效的 C# 语句。

将表达式的值分配给变量:

var q = from p in ... ;

或者删除末尾的分号并将语言更改为C# 表达式

Your code is not a valid C# statement.

Either assign the value of your expression to a variable:

var q = from p in ... ;

Or remove the semicolon at the end and change the Language to C# Expression

枕头说它不想醒 2024-12-20 20:33:26

您需要将其分配给某些东西。

var productIds = from p in Products where p.UnitPrice > 50 select new {p.ProductID };

You need to assign it to something.

var productIds = from p in Products where p.UnitPrice > 50 select new {p.ProductID };
泛滥成性 2024-12-20 20:33:26

对我来说唯一看起来奇怪的是,在新的匿名对象中,您没有将 ProductID 分配给属性,但我想这并不是真正需要的(我刚刚尝试过)。

from p in Products where p.UnitPrice > 50 select new { ProductID = p.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).

from p in Products where p.UnitPrice > 50 select new { ProductID = p.ProductID };

EDIT: Due to the comments, I realize this will not provide anything useful for solving the the OP's problem.

青柠芒果 2024-12-20 20:33:26

您正在尝试在分配后进行选择。所以你应该使用这样的东西

from p in Products where p.UnitPrice > 50 select 
                                                new { 
                                                      ProductID = p.ProductID 
                                                    };

you are trying to select after getting it assigned so . so you should use some thing like this

from p in Products where p.UnitPrice > 50 select 
                                                new { 
                                                      ProductID = p.ProductID 
                                                    };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文