Results 1 to 9 of 9

Thread: Variable Select Statement in LINQ

  1. #1

    Thread Starter
    Lively Member Christhemist's Avatar
    Join Date
    Sep 2016
    Location
    Nevada
    Posts
    116

    Variable Select Statement in LINQ

    I am trying to recreate an Access/SQL like view builder in VB.net

    I have two DataTables, AddressListDataTable and MatrixDataTable

    These two DataTables will be combined with LINQ and added to a third DataTable called CombinedDataTable

    My Issue is that the fields in both the AddressListDataTable and MatrixDataTable will change often, so I need to figure out how to build a custom Select Statement.

    Here is my code: (Highlighted is the line that will change often because the field names will change)

    Code:
    Dim both = From row1 In AddressListDatatable.AsEnumerable()
                       Join row2 In MatrixDatatable.AsEnumerable()
                       On row1.Field(Of String)("Offercode") Equals row2.Field(Of String)("Code")
                       Select row1("Account") & "|" & row1("First") & "|" & row1("OFFERCODE") & "|" & row2("Expiration") & "|" & row2("OfferDescription")
    The highlighted row actually works, but it is a manual solution. I will be selecting different fields each time because the AddressListDataTable and MatrixDataTable content will be populated with different data and column names each time.

    So, how do I create a custom select statement each time the code is ran?

    Also, here is how I am adding the data to the CombinedDataTable:

    Code:
    For Each r1r2 In both
    
                Dim words As String() = r1r2.Split(New Char() {"|"c})
                
                CombinedDataTable.Rows.Add(words)
    
            Next

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Variable Select Statement in LINQ

    Code:
    Select row1(a string variable) & "|" & ...

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Variable Select Statement in LINQ

    Will there always be the same number of columns selected?

  4. #4
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: Variable Select Statement in LINQ

    Do you mean like this:

    VB.Net Code:
    1. Dim both = From row1 In AddressListDatatable.AsEnumerable()
    2.            Join row2 In MatrixDatatable.AsEnumerable()
    3.            On row1.Field(Of String)("Offercode") Equals row2.Field(Of String)("Code")
    4.            Select Join(AddressListDatatable.Columns.OfType(Of DataColumn).Select(Function(x) $"{row1(x.ColumnName)}").ToArray, "|") & "|" &
    5.                   Join(MatrixDatatable.Columns.OfType(Of DataColumn).Select(Function(x) $"{row2(x.ColumnName)}").ToArray, "|")

    Kris

  5. #5

    Thread Starter
    Lively Member Christhemist's Avatar
    Join Date
    Sep 2016
    Location
    Nevada
    Posts
    116

    Re: Variable Select Statement in LINQ

    @.paul That's what I tried but it doesn't like the quotes within the string

    @jmcilhinney it will not always be the same amount of columns, that will vary each time.

    @i00 I believer your solution selects all of the columns from both tables. I need to only select certain columns from the join statement.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Variable Select Statement in LINQ

    Quote Originally Posted by Christhemist View Post
    it will not always be the same amount of columns, that will vary each time.
    In that case, you should be able to do something like this:
    vb.net Code:
    1. Dim addressListColumnNames As New List(Of String)
    2. Dim matrixColumnNames As New List(Of String)
    3.  
    4. 'Add the names of the columns you want to project to the appropriate lists.
    5.  
    6. Dim both = From row1 In AddressListDatatable.AsEnumerable()
    7.            Join row2 In MatrixDatatable.AsEnumerable()
    8.            On row1.Field(Of String)("Offercode") Equals row2.Field(Of String)("Code")
    9.            Select String.Join("|",
    10.                               addressListColumnNames.Select(Function(alcn) row1(alcn)).
    11.                                                      Concat(matrixColumnNames.Select(Function(mcn) row2(mcn))))

  7. #7

    Thread Starter
    Lively Member Christhemist's Avatar
    Join Date
    Sep 2016
    Location
    Nevada
    Posts
    116

    Re: Variable Select Statement in LINQ

    That worked perfectly!

    My only complaint now is that for large recordsets it takes longer than I'd like to loop through the data to split it into the CombinedDataTable fields. As seen here:

    Code:
     
    
            Dim DataCount As Integer = 1
    
            ProgressBar1.Maximum = both.Count
        
            For Each r1r2 In both
    
                ProgressBar1.Value = DataCount
    
                Dim words As String() = r1r2.Split(New Char() {"|"c})
    
                CombinedDataTable.Rows.Add(words)
    
                DataCount = DataCount + 1
    
            Next

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Variable Select Statement in LINQ

    Why create a String that needs splitting in the first place?
    vb.net Code:
    1. Dim addressListColumnNames As New List(Of String)
    2. Dim matrixColumnNames As New List(Of String)
    3.  
    4. 'Add the names of the columns you want to project to the appropriate lists.
    5.  
    6. Dim both = From row1 In AddressListDatatable.AsEnumerable()
    7.            Join row2 In MatrixDatatable.AsEnumerable()
    8.            On row1.Field(Of String)("Offercode") Equals row2.Field(Of String)("Code")
    9.            Select addressListColumnNames.Select(Function(alcn) row1(alcn)).Concat(matrixColumnNames.Select(Function(mcn) row2(mcn)))
    10.  
    11. Dim combinedDataTable As New DataTable
    12.  
    13. 'Add columns to combinedDataTable here.
    14.  
    15. For Each result In both
    16.     combineDataTable.Rows.Add(result.ToArray())
    17. Next
    Not only will that be faster but it will maintain the same data types too.

  9. #9

    Thread Starter
    Lively Member Christhemist's Avatar
    Join Date
    Sep 2016
    Location
    Nevada
    Posts
    116

    Re: Variable Select Statement in LINQ

    Great idea!

    I am getting this error when trying it though: "Input array is longer than the number of columns in this table."


    Edit: Nevermind, I didn't have any columns added to the DataTable... my bad.

    Thanks for all your help!
    Last edited by Christhemist; May 10th, 2018 at 03:41 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width