Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate two column values in LinQ Lambda Expression

Tags:

c#

sql

lambda

linq

I am new to LinQ and those lambdas are appearing tricky to me :(

I have a table where there are two columns. First_Name and Last_name. I am populating a gridview using LinQ.

protected void Page_Load(object sender, EventArgs e)
    {
        myLinQtoSQLClassDataContext objDataContext = new myLinQtoSQLClassDataContext();

        var allUserList = from CurrentUser in objDataContext.Users.Where(c => c.Is_Deleted != false)                              
                          select new
                          {
                              CurrentUser.First_Name, 
                              CurrentUser.Last_Name,
                              CurrentUser.Email_ID,
                              CurrentUser.GUID
                          };

        GridView1.DataSource = allUserList;
        GridView1.DataBind();                              
    }

I can retrieve the values using LinQ but I want to concatenate the first name and last name with a space in between.

The equivalent SQL query what I am trying to acchieve would be like this:

Select First_name + ' ' + Last Name as Username, Email_ID, GUID
From tbl_Users where Is_Deleted != false

How can I achieve this through the lambda expression?

like image 219
Manas Saha Avatar asked Apr 19 '12 09:04

Manas Saha


1 Answers

You can use string concatenation:

select new
{
    Username = CurrentUser.First_Name + " " + CurrentUser.Last_Name,
    CurrentUser.Email_ID,
    CurrentUser.GUID
};
like image 106
Mark Byers Avatar answered Sep 24 '22 06:09

Mark Byers