14

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?

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
Manas Saha
  • 1,477
  • 9
  • 29
  • 44

7 Answers7

24

You can use string concatenation:

select new
{
    Username = CurrentUser.First_Name + " " + CurrentUser.Last_Name,
    CurrentUser.Email_ID,
    CurrentUser.GUID
};
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
4

Try

     select new
            {
                          FullName = CurrentUser.First_Name + " " + CurrentUser.Last_Name,
                          CurrentUser.Email_ID,
                          CurrentUser.GUID
            };
Mithrandir
  • 24,869
  • 6
  • 50
  • 66
  • Thank you everyone, all the solutions work fine. I wish I could mark all of them as correct but I can only mark one :( – Manas Saha Apr 19 '12 at 09:31
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
                  };
Habib
  • 219,104
  • 29
  • 407
  • 436
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.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
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 
}; 
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
Parv Sharma
  • 12,581
  • 4
  • 48
  • 80
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});
Bruno
  • 533
  • 1
  • 6
  • 27
  • 2
    `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 – Charly H Apr 26 '17 at 07:03
0
select new
{
    Username = string.Format("{0} {1}", CurrentUser.First_Name, CurrentUser.Last_Name),
    CurrentUser.Email_ID,
    CurrentUser.GUID
};