Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 387
ghanshyam.mirani Avatar asked Dec 16 '22 08:12

ghanshyam.mirani


2 Answers

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 };
like image 141
Jon Skeet Avatar answered Dec 30 '22 22:12

Jon Skeet


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)

like image 36
Pranay Rana Avatar answered Dec 30 '22 22:12

Pranay Rana