Jean Paul Olvera
Jean Paul Olvera

Reputation: 399

Linq with alias

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.

Upvotes: 25

Views: 48609

Answers (6)

hadiz7
hadiz7

Reputation: 41

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

Upvotes: 4

Servy
Servy

Reputation: 203834

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

Upvotes: 6

D Stanley
D Stanley

Reputation: 152566

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

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1501043

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"

Upvotes: 11

cdhowie
cdhowie

Reputation: 169068

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

Upvotes: 45

Reed Copsey
Reed Copsey

Reputation: 564451

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

Upvotes: 7

Related Questions