ghanshyam.mirani
ghanshyam.mirani

Reputation: 3101

Give a Specific name to column in Linq

I have following query,

var employees = from emp in this.DataWorkspace.v2oneboxData.me_employees
                where emp.me_is_deleted.Value == false && emp.me_is_manager == true
                select new{emp.me_first_name,emp.me_last_name,emp.me_pkey};

I want to give a specific name to emp.me_first_name column, just like we do in SQL Queries like:

select emp_first_Name as "First Name" from me_employees

how to do it in linq queries ???

also is it possible to combine firstName and lastname in select query in Linq ? just like we do in SQL Queries like :

select me_first_name + ' ' + me_last_name as 'Full Name' from me_employee

how can i achieve this task in linq?

Thanks

Upvotes: 4

Views: 1517

Answers (3)

Pranay Rana
Pranay Rana

Reputation: 176956

var list = fro e in emp
           select new { FullName = e.Firstname + " " + e.LastName }

One of the example but its with the group by

enter image description here Check more at : Learn SQL to LINQ (Visual Representation)

Upvotes: 4

George Duckett
George Duckett

Reputation: 32448

from emp in this.DataWorkspace.v2oneboxData.me_employees
where emp.me_is_deleted.Value == false && emp.me_is_manager == true
select new{FullName = String.Format("{0} {1}",emp.me_first_name, emp.me_last_name),emp.me_pkey};

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1503449

You can't make it First Name as that isn't a valid identifier, but you can use:

select new { FirstName = emp.me_first_name,
             LastName = emp.me_last_name,
             Key = emp.me_pkey };

Basically the property name in the anonymous type only defaults to whatever property you're using as the value - you can specify it yourself, but it has to be a valid C# identifier.

Or for the combination:

select new { FullName = emp.me_first_name + " " + emp.me_last_name,
             Key = emp.me_pkey };

Upvotes: 6

Related Questions