-
May 9th, 2018, 11:32 AM
#1
Thread Starter
Lively Member
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' "
-
May 9th, 2018, 08:54 PM
#2
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.
-
May 9th, 2018, 09:52 PM
#3
Thread Starter
Lively Member
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})")
-
May 9th, 2018, 10:15 PM
#4
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:
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.
-
May 9th, 2018, 10:29 PM
#5
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|