简体   繁体   中英

Concatenate two column values in LinQ Lambda Expression

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?

You can use string concatenation:

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

Try

     select new
            {
                          FullName = CurrentUser.First_Name + " " + CurrentUser.Last_Name,
                          CurrentUser.Email_ID,
                          CurrentUser.GUID
            };
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
                  };

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.

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

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});
select new
{
    Username = string.Format("{0} {1}", CurrentUser.First_Name, CurrentUser.Last_Name),
    CurrentUser.Email_ID,
    CurrentUser.GUID
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM