Results 1 to 5 of 5

Thread: How do I define field names in a LINQ Dynamic Select Statement?

  1. #1

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

    How do I define field names in a LINQ Dynamic Select Statement?

    I am creating dynamic select statement in a LINQ Query using the System.Linq.Dynamic reference.

    However, I am having syntax issues with defining the field names I want selected from the query.

    Code:

    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, row2)
            Dim data = both.Select("new (row1.Account, row1.First, row1.OFFERCODE, row2.OFFER, row2.Expiration)")
    The highlighted portion of the code is where I am having syntax issues. This is the error it is giving me " No property or field 'Account' exists in type 'DataRow' "

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

    Re: How do I define field names in a LINQ Dynamic Select Statement?

    Are you using a typed DataSet? If not then, as the error message says, the DataRow objects you're referring to have no properties with those names. What they do have is an Item property that you can index by ordinal or column name.

    By the way, I have replied to another of your threads with a way to get the data you want without using DLINQ.

  3. #3

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

    Re: How do I define field names in a LINQ Dynamic Select Statement?

    I saw your other reply and I will respond to that after this. Whichever way I can get this working makes no difference to me... at least for now lol.

    It is not a typed DataSet, I am importing the data into several DataTables via dbase connection string. Which is why it can't find the headers I have highlight above, but...

    I have tried to call the item by index and could not get anything to work, Here is a quick list of formats I used. I will only select one field as an example to show you the synatx I tried.

    Code:
     Dim data = both.Select("new (row1(0))")
    
    Dim data = both.Select("new (row1{(0)})")
    
    Dim data = both.Select("new (row1.(0))")
    
    Dim data = both.Select("new (row1.Item(0))")
    
    Dim data = both.Select("new ({row1.Account})")

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

    Re: How do I define field names in a LINQ Dynamic Select Statement?

    I haven't used DLINQ much at all but I wouldn't expect lines 2, 3 or 5 to work simply because they make no sense. If DLINQ requires C# syntax then you might have to do something like this:
    vb.net Code:
    1. Dim data = both.Select("new (row1[0])")
    C# uses indexers rather than default properties. I'll have a go at this myself some time today and see if I can come up with a DLINQ solution. That said, if you want to go by an arbitrary set of column names then I think my solution in your other thread is probably better anyway.

  5. #5

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

    Re: How do I define field names in a LINQ Dynamic Select Statement?

    You were really close, it wanted the "as" statement

    Code:
    Dim data = both.Select("new (row1[0] as Account))
    Unfortunately, this would become a cumbersome parsing project next, thus taking longer to process large recordsets. I believe your solution on my other thread was much more ideal in this regard.

    Here is the link to that thread for anyone interested: http://www.vbforums.com/showthread.p...tement-in-LINQ

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