Linq with alias

Jean Paul Olvera picture Jean Paul Olvera · Apr 9, 2013 · Viewed 36.5k times · Source

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.

Answer

cdhowie picture cdhowie · Apr 9, 2013

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