3

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.

8
  • 8
    Can you post the rest of the code, please?
    – MGZero
    Nov 4, 2011 at 15:03
  • Is the code in an application or are you actually using LINQ Pad (saw the linqpad tag...it can make a difference)? Nov 4, 2011 at 15:04
  • What is your language setting in LINQPad?
    – mwilson
    Nov 4, 2011 at 15:04
  • updated question with more details Nov 4, 2011 at 15:06
  • If you are in "C# Expression" language mode, you have to drop the semi-colon in your first example. See my post below.
    – Alex Moore
    Nov 4, 2011 at 15:19

8 Answers 8

9

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();
5
  • LINQPAD has a mode, Expression that executes statements just as he wrote them (minus the trailing semicolon). I presume that is how s/he expected it to work.
    – Marc
    Nov 4, 2011 at 15:16
  • He/She is trying out the expression in LINQPad, not in C# as a statement.
    – Alex Moore
    Nov 4, 2011 at 15:16
  • LINQPAD has a Lanuage combo, I have choosen "c# statment" from there. Just to be clear, there is another option "C# Expression" but that is NOT what i am using in this case. Nov 4, 2011 at 15:19
  • 2
    @Marc: LINQPad statement mode, just like the C# compiler, does not accept query expressions trying to masquerade as statements (by ending them with a semicolon).
    – Ani
    Nov 4, 2011 at 15:20
  • @Ani: Yes Its true I am completely new to Linq and struggling to go through the initial hump. Thanks for your help. Nov 4, 2011 at 16:28
6

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.

2
  • I added an example in question where I can run it as "C# statment" without assigning it back to a var Nov 4, 2011 at 15:19
  • I get '; expected' if I try to run the second example as a C# statement.
    – Alex Moore
    Nov 4, 2011 at 15:24
1
            var db = new DataContext();
            var product= from p in db.Products 
                          where p.UnitPrice > 50
                          select new{ProductId = p.ProductID };
0

Try assigning the result to a variable.

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

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

0

You need to assign it to something.

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

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.

4
  • 4
    You dont need to specify a property name if you are assigning a field or property, and you want that name still. Very handy indeed!
    – leppie
    Nov 4, 2011 at 15:08
  • 3
    new { p.ProductID } and new { ProductID = p.ProductID } will produce the same anonymous type.
    – cadrell0
    Nov 4, 2011 at 15:08
  • @cadrell0: Didn't know that (updated my answer)! I guess I just always like to be explicit with those darned anonymous types :)
    – Cᴏʀʏ
    Nov 4, 2011 at 15:10
  • I used to always be explicit, then I got ReSharper and it started scolding me.
    – cadrell0
    Nov 4, 2011 at 15:13
-2

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 
                                                    };
1
  • That's the same code except you have some new lines. And a redundant property name in your anonymous type.
    – cadrell0
    Nov 4, 2011 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.