C# 在 LinQ Lambda 表达式中连接两列值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10225131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:57:31  来源:igfitidea点击:

Concatenate two column values in LinQ Lambda Expression

c#sqllinqlambda

提问by Manas Saha

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. 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 表达式实现这一点?

采纳答案by Mark Byers

You can use string concatenation:

您可以使用字符串连接:

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

回答by Mithrandir

Try

尝试

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

回答by Grant Thomas

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.

然后在分配用户名时连接字符串。

回答by Habib

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

回答by Parv Sharma

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

看看这个CLR Method to Canonical Function Mapping
.Net 提供了许多可以直接映射到查询的方法,你必须使用其中之一来添加两个字符串,
所以你可以使用的是

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

回答by Bruno

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

回答by tselvakumars

select new
{
    Username = string.Format("{0} {1}", CurrentUser.First_Name, CurrentUser.Last_Name),
    CurrentUser.Email_ID,
    CurrentUser.GUID
};