Link to home
Start Free TrialLog in
Avatar of isischen
isischen

asked on

I need someone to explain parts of a LINQ query...

I am not sure of the syntax of a LINQ query, please identify the following items:
1.  From f In Th.AsEnumerable()   ' can "f" be any name I set it to?
2.  In th.AsEnumerable() ' is "th" the datatable that I need to find the data using the subsequent 'Select" operator?
3.  f.Field(of string)("vocab")  ' in this code segment, is ' vocab ' one of the field in the ' th ' dataset datatable?
4.  dim thquery as object  ' can I declare thquery as an object inside the LINQ contex?
5.  For Each f in thquery  ' in the place of and the content of f need to be the same as the f in above item #1
6.  WHY is my LINQ query won't ge executed in my For Each f in thquery...Next loop?
Dim tiu As DataTable = ds.Tables(DtTableN)
        Dim db As New dbhDataSet()
        Dim Th As DataTable = db.Tables("th")
        dim thquery as object
        Dim row As DataRow
For Each row In tdt.Rows
thquery = From f In Th.AsEnumerable() Where f.Field(Of String)("vocab") = row(0).ToString() AndAlso Not f.Field(Of String)("vocab") = IsDBNull(row(0).ToString()) Select New With {.v = f.Field(Of String)("vocab")}
next
For Each f In thquery
            myrowTh = _vDtTh.NewRow()
            myrowTh(mycol) = f.v
            myrowTh(mycolgPt) = f.g
            myrowTh(mycolsubHead) = f.s
            myrowTh(mycolkey) = f.ke
            _vDtTh.Rows.Add(myrowTh)
        Next

Open in new window

Avatar of naspinski
naspinski
Flag of United States of America image

1.  Yes, 'f' can be anything.  Sometimes it helps to be more descriptive with your name.  You will just have to keep it consistent within the query itself -- change 'f' and the beginning, change it throughout the query

2. 'Th.AsEnumerable()' is teh set of data you are searching through in the following statement

3. Yes

4. It is even easier, the new 3.5 architecture uses inference.  SO, youc an get rid of that whole line and instead just do this:
Dim thquery = From f In ...

5. 'f' in this case can be anything you want it to be, and does not relate tot eh 'f' above.  Each 'f' is in it's own statement and knows nothing of the other one.

6. Not totally sure I understand this question, but: The LINQ query calls it one time and makes a collection so you do not need to make multiple trips to the DB.

Please let me know if you need any more explanation on anything.
Avatar of isischen
isischen

ASKER

Thank you for answering question 1 thru 5. The last question is to seek an expert's eye to go through my code snippet to find bug because when I debug  the for each...next code block was skipped over---
         For Each f In thquery
            myrowTh = _vDtTh.NewRow()
            myrowTh(mycol) = f.v
            myrowTh(mycolgPt) = f.g
            myrowTh(mycolsubHead) = f.s
            myrowTh(mycolkey) = f.ke
            _vDtTh.Rows.Add(myrowTh)
        Next
so, no result derived from my LINQ query!
Just before I post this comment, I add 'system.data.linq' and 'Microsoft.visual basic.linq' to project reference but still not able to get any data out of the 'Th' dataset datatable!
Any idea of where else to look for bugs?
You are correct it is not returning anything.
Have you verified that TH has data inside of it?  Try binding it to a gridview to make sure.
Yes, there is data inside the Th datatable when do a preview of dataset. However, there are more then 2 row of voc column match one row(0).tostring. And I have the following imports statements:
Imports System.Data
Imports System.Data.DataSet
Imports System.Data.OleDb
Imports Microsoft.Office.Interop.Access
Imports Microsoft.Office.Interop.Word
Imports System
Imports System.Collections
Imports System.Collections.Specialized
Imports System.Linq
Imports System.Linq.Expressions
Imports System.Data.Common
Imports System.Collections.Generic
Imports System.Text
Imports System.Data.SqlClient
Also, I downloaded LINQ PREVIEW yesterday but have not read it in order to use it.

I can't bind result to gridview with this error message:
Unable to cast object of type 'System.Data.EnumerableRowCollection`1[VB$AnonymousType_1`4[System.String,System.String,System.String,System.String]]' to type 'System.Data.EnumerableRowCollection`1[System.String]'.

I have set the argument type in the receiving sub to be a System.Data.EnumerableRowCollection(Of String) and receive the above error message.
Thank you.
at this post in expert-exchange:
https://www.experts-exchange.com/questions/23335709/Linq-To-Sql-Dynamic-Query-using-Collection-Object-Visual-Studio-2008.html - 77k -
it use a microsoft reporting service...I wonder if I need to use that or if I can just add
this:
//Get Connection String from App.Config
                string connStr = System.Configuration.ConfigurationManager.ConnectionStrings
                                           ["MyConnectionString"].ConnectionString;

                //Get Database Connection
                DALDataContext DataContext = new DALDataContext(connStr);
                               
                // create typed table
                Table<Appointment> appointments = DataContext.GetTable<Appointment>();

Is the latter of something necessary?
you are not connecting to anything from what it looks like... why would you need that?
looking at your query, it seems that you are looking for a single row, am I correct?

if that is teh case, your query would do a lot of unnecessary checking, try something like this (just for testing at least now)

thquery = (From f In Th.AsEnumerable() select f).First()

that will just pull the first row.

the foreach is really unnecessary, as you are only going o run it once.
I tried to do this---dim boundtable as datatable=thquery.copytodatatable()
but my intellisense did not provide the copytodatatable option when I typed thquery. '?
Maybe I missed to add reference to or imports something I am not aware of!
I did not get a result from tyring something similar to what you suggested--
thquery = (From f In Th.AsEnumerable() Where f.Field(Of String)("voc") = "jones" Select New With {.v = f.Field(Of String)("voc").First()})
Debugging will skip over the code block of:
            For Each f In thquery
            Console.WriteLine("{0}", f.v.ToString)
            next
And, when I tried to do without the 'For each f in thquery', I did not know how else to replace 'console.writeline("{0}", f.v.tostring)
Thank you.
I received "System.Data.EnumerableRowCollection`1[VB$AnonymousType_0`1[System.Char]]"
as the output of ' console.writeline("{0}",thquery.Tostring) ' when I am expecting a "199" string datatype.

ASKER CERTIFIED SOLUTION
Avatar of naspinski
naspinski
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
should list out all the "vocab" values in the table
I read the LINQ PREVIEW and did f!vocab and ruin my project totally. I now have error in the visual studio generated class!
With vb.net 2008 express version, I can't see the datatable.LOADSEQUENCY(query) on my intellisense.
Thinking of download the VISAUL STUDIO 2008 professional (trial).  What do you think?
Not completely following what happened, but VWD will do just fine.  Maybe uninstall and reinstall it?

VS is great, but it is expensive and large (it is what I use and recommend).  It is up to you.
Thank you for folllowing up with me. Anyway, I have tried to uninstall and reinstall Visual Basic.net 2008 express.  Having trouble with 'add datasource wizard' it keep giving me a 'can't establish relation' message at the end of click 'finish'. BUT, I can see my data using 'preview data'. So, I went ahead and revise my code to read as
 dim thquery= from f in In dt.AsEnumerable() Select f
and also tried this
 dim thquery= from f in In dt.AsEnumerable() where f("key")="away" Select new with {.s=f("subHead")}
all come the rest with the yellow hightlight on this
 for each f in thquery
with a invalidoperationexception error of 'sequence contain no element'
Oh, I found out that if I add reference to 'system.data.extension' it will cause the vb.net generated code to become full of errors.
Thank you.

 
ok, that just means that your query is not returning any data, therefore it means that dt doesn't have any data inside of it.