简体   繁体   English

连接LinQ Lambda Expression中的两个列值

[英]Concatenate two column values in LinQ Lambda Expression

I am new to LinQ and those lambdas are appearing tricky to me :( 我是LinQ的新手,那些lambdas对我来说显得很棘手:(

I have a table where there are two columns. 我有一张表有两列。 First_Name and Last_name. First_Name和Last_name。 I am populating a gridview using LinQ. 我正在使用LinQ填充gridview。

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. 我可以使用LinQ检索值,但我想连接名字和姓氏,中间有空格。

The equivalent SQL query what I am trying to acchieve would be like this: 我试图改进的等效SQL查询将是这样的:

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? 我怎样才能通过lambda表达式实现这一目标?

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): 你应该给你的匿名类型 'keys'(只读属性):

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 看看这个CLR方法到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 .Net提供了许多可以直接映射到查询的方法,我们必须使用其中一个来添加两个字符串
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
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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