Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq with alias

Tags:

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.

like image 604
Jean Paul Olvera Avatar asked Apr 09 '13 16:04

Jean Paul Olvera


People also ask

How to give alias name in LINQ query?

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.

How do I use alias with same select statement?

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 := .

Can I use alias in WHERE clause mysql?

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.

What is alias in mysql?

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.


1 Answers

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 }); 
like image 69
cdhowie Avatar answered Sep 30 '22 12:09

cdhowie