23

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.

Servy
  • 202,030
  • 26
  • 332
  • 449
Jean Paul Olvera
  • 379
  • 1
  • 4
  • 9

6 Answers6

44

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 });
cdhowie
  • 158,093
  • 24
  • 286
  • 300
11

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.surname
           select new { x.id_author, fullName };

Note that x.name + " " + x.surname will be compiled to the same code as String.Concat(x.name, " ", x.surname), but is more readable to most people. Also note that as you're not doing anything outside the () parentheses, there's no need for them.

I would hope that any good SQL LINQ provider should turn this query into a sensible and efficient SQL query, but you should validate this yourself. On the other hand, I would generally suggest preferring querying over individual fields, e.g.

where x.name == "Jean Paul" && x.surname == "Olvera"
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
7

You haven't created it yet.

That being said, since you know the name in advance, you should be able to perform the query on the parts:

var name = from x in db.authors
                where name == "Jean Paul" && surname == "Olvera"
                orderby x.surname
                select new { x.id_author, fullName= String.Concat(x.name," ", x.surname) };

Otherwise, you can use let to do this:

var name = from x in db.authors
           let fullName = String.Concat(x.name," ", x.surname)
           where fullName == "Jean Paul Olvera"
           orderby x.surname
           select new { x.id_author, fullName=fullName ) };
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
5

It's easier in the method syntax, as you aren't constrained to the order of the operations:

var query = authors.OrderBy(x => x.surname)
    .Select(x => new
    {
        x.id_author,
        fullName = String.Concat(x.name, " ", x.surname)
    })
    .Where(x => x.fullName == "Jean Paul Olvera");
Servy
  • 202,030
  • 26
  • 332
  • 449
4
linq1DataContext ll = new linq1DataContext();
            if (comboBox1.SelectedIndex == 0)
            {

                var q = from m in ll.personals                            



                        let کد= m.id
                        let نام = m.name

                        select new { 
                                     کد,
                                     نام,
                        };
                dataGridView1.DataSource = q;
            }
hadiz7
  • 41
  • 1
  • 2
2

use the let clause:

var name = (from x in db.authors
                let fullName = String.Concat(x.name," ", x.surname)
                where fullname = "Jean Paul Olvera"
                orderby x.surname
                select new { x.id_author, fullName });
D Stanley
  • 149,601
  • 11
  • 178
  • 240