I have the following line in c#:
var name = (from x in db.authors where fullName == "Jean Paul Olvera" orderby x.surname select new { x.id_author, fullName= String.Concat(x.name," ", x.surname) });
my problem is I want to use the alias in my where clause, but I can't, 'fullName' appears as not declared.
You need to put that part of the projection earlier, which is easy with a let clause: var name = from x in db. authors let fullName = x.name + " " + x. surname where fullName == "Jean Paul Olvera" orderby x.
No there isn't a way to refer to aliases, but you can assign the expression to a variable, and then refer to the variable in the same select clause. Inside a select statement variable assignment is always done by the infix operator := . *In a SET statement, it can be either = or := .
You can only use column aliases in GROUP BY, ORDER BY, or HAVING clauses. Standard SQL doesn't allow you to refer to a column alias in a WHERE clause. This restriction is imposed because when the WHERE code is executed, the column value may not yet be determined.
SQL aliases are used to give a table, or a column in a table, a temporary name. Aliases are often used to make column names more readable. An alias only exists for the duration of that query. An alias is created with the AS keyword.
You can use let
to create intermediate values:
var name = (from x in db.authors let fullName = x.name + " " + x.surname where fullName == "Jean Paul Olvera" orderby x.surname select new { x.id_author, fullName });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With