14

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 achieve 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?

1
  • 1
    In SQL you should normally use ' ' to mean a string containing a space., not " ". Commented Apr 19, 2012 at 9:26

7 Answers 7

25

You can use string concatenation:

select new
{
    Username = CurrentUser.First_Name + " " + CurrentUser.Last_Name,
    CurrentUser.Email_ID,
    CurrentUser.GUID
};
Sign up to request clarification or add additional context in comments.

Comments

4

Try

select new
{
    FullName = CurrentUser.First_Name + " " + CurrentUser.Last_Name,
    CurrentUser.Email_ID,
    CurrentUser.GUID
};

Comments

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

Comments

1

You should give your anonymous type 'keys' (read-only properties):

select new
{
  Name = CurrentUser.First_Name + " " + CurrentUser.Last_Name,
};

And then just concatenate the string on assigning the user name.

Comments

1

have a look at this CLR Method to Canonical Function Mapping
.Net provides many methods that can be directly mapped to the queries ull have to use one of them to add two strings
so one that u can use is

select new 
{ 
    Username = Concat(first_Name,Last_Name), 
    CurrentUser.Email_ID, 
    CurrentUser.GUID 
}; 

Comments

1

Here's another variation that works and has not been listed:

var allUserList =  objDataContext.Users.Where(c => c.Is_Deleted != false).
     Select(s => new{First_Name + " " + Last_Name, Email_ID, GUID});

1 Comment

var allUserList = objDataContext.Users.Where(c => c.Is_Deleted != false).Select(s => new{FullName = First_Name + " " + Last_Name, Email_ID, GUID}); worked for me. I needed to add FullName as a Field
0
select new
{
    Username = string.Format("{0} {1}", CurrentUser.First_Name, CurrentUser.Last_Name),
    CurrentUser.Email_ID,
    CurrentUser.GUID
};

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.