Results 1 to 4 of 4

Thread: How do I select variable fields in a LINQ Select statement?

Threaded View

  1. #1

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

    How do I select variable fields in a LINQ Select statement?

    The fields in which will be selected will change each time. Also, I am selecting from several lists so it makes it hard to use a string list.

    Here is my code: (Note: This works manually but once the field names change it will not work.)

    Code:
    Dim both = From row1 In AddressListDatatable.AsEnumerable()
                       Join row2 In Matrix1Datatable.AsEnumerable()
                       On row1.Field(Of String)("Offercode") Equals row2.Field(Of String)("Code")
                       Select row1("Account") & "," & row1("First") & "," & row2("CODE") & "," & row2("SWITCH") 
    As you can see I am selecting different fields from two DataTables. However, the name and quantity of these fields will change often. I came up with the following solution but it won't work with multiple lists. (Note: I already populated the string list "ListOfColumnNames" prior to running this code)

    Here is that code:

    Code:
    Dim both = From row1 In AddressListDatatable.AsEnumerable()
                       Join row2 In Matrix1Datatable.AsEnumerable()
                       On row1.Field(Of String)("Offercode") Equals row2.Field(Of String)("Code")
                       Select String.Join(", ", From i In ListOfColumnNames Select row1(i))
    Last edited by Christhemist; May 7th, 2018 at 06:05 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