Silverlight Student
Silverlight Student

Reputation: 4118

What is wrong with this statement below

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.

Upvotes: 3

Views: 5345

Answers (8)

Cᴏʀʏ
Cᴏʀʏ

Reputation: 107606

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.

Upvotes: 0

Ani
Ani

Reputation: 113462

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();

Upvotes: 9

Alex Moore
Alex Moore

Reputation: 3465

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.

Upvotes: 6

Adam Barney
Adam Barney

Reputation: 2387

You need to assign it to something.

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

Upvotes: 0

Jan
Jan

Reputation: 16038

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

Upvotes: 0

Purplegoldfish
Purplegoldfish

Reputation: 5294

Try assigning the result to a variable.

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

Upvotes: 0

huMpty duMpty
huMpty duMpty

Reputation: 14470

            var db = new DataContext();
            var product= from p in db.Products 
                          where p.UnitPrice > 50
                          select new{ProductId = p.ProductID };

Upvotes: 1

Hari Gillala
Hari Gillala

Reputation: 11926

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 
                                                    };

Upvotes: -2

Related Questions