none
Use of column alias in LINQ RRS feed

  • Question

  • Quick question about LINQ.  I get getting some AD user data and have the following code

    dgvSearchResults.DataSource = (From res As UserPrincipal In results
                                    Select res.Name, res.DisplayName, res.EmailAddress).ToList

    Is there a way to change the column name 'NAME' to something like 'UserID'?  

    Thanks


    Brad Allison

    Thursday, May 23, 2019 11:06 AM

Answers

  • Hello,

    Here is the syntax when using the results in the same method, here I use a DataTable declared as dt.

    Dim results = dt.AsEnumerable().Select(Function(row) New With {.UserId = row.Field(Of String)("Name")})

    If the result is needed in another sub or function we need to create a public class  e.g.

    Public Class Example
        Public Property UserId() As String
    End Class

    Then 

    Dim results = dt.AsEnumerable().Select(Function(row) New Example With {.UserId = row.Field(Of String)("Name")})
    All the above requires framework 3.5 or higher.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, May 23, 2019 11:16 AM
    Moderator

All replies

  • Hello,

    Here is the syntax when using the results in the same method, here I use a DataTable declared as dt.

    Dim results = dt.AsEnumerable().Select(Function(row) New With {.UserId = row.Field(Of String)("Name")})

    If the result is needed in another sub or function we need to create a public class  e.g.

    Public Class Example
        Public Property UserId() As String
    End Class

    Then 

    Dim results = dt.AsEnumerable().Select(Function(row) New Example With {.UserId = row.Field(Of String)("Name")})
    All the above requires framework 3.5 or higher.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, May 23, 2019 11:16 AM
    Moderator
  • Thanks for that information

    Brad Allison

    Thursday, May 23, 2019 11:23 AM