-
Aug 8th, 2013, 10:41 PM
#1
Thread Starter
Member
[RESOLVED] Need advice with LINQ code
I am trying to find out what I did wrong with the following code.
PHP Code:
Dim filter1 = From Electrical In ElectricalPartsDataSet.Electrical _
Where Electrical.Product = "Resistor" _
Select Electrical.Product, Electrical.Name, Electrical.Picture, Electrical.Pdf
Me.ElectricalDataGridView.DataSource = filter1
My dataset name is "ElectricalParts" and the table name is "Electrical". The table consists only the fields that are in the select statement. Its small until I get the filtering working and understand it.
I placed this in a button just to run it. Eventually attaching it to a listbox that will contain different electrical products and the "Resistor" will be changed to reflect the listbox selection. This way the datagridview will filter accordingly.
I have 2 lines filled in the database, one of them is "Resistor", when the above code runs, the datagridview displays nothing.
Thanks,
PS. If additional info is required please let me know. I tried to cover it all, but might of missed something.
-
Aug 8th, 2013, 10:53 PM
#2
Re: Need advice with LINQ code
I would say that the immediate issue is that 'filter1' is an IEnumerable and the DataSource of a DataGridView requires an IList. If you use 'filter1.ToArray()' instead then I think it will work.
That said, you really shouldn't be using LINQ to filter a DataTable in a DataGridView anyway. I would suggest binding the DataTable to a BindingSource and binding that to the DataGridView, then setting the Filter property of the BindingSource to filter the data, e.g.
Code:
myBindingSource.Filter = "Product = 'Resistor'"
-
Aug 9th, 2013, 08:37 AM
#3
Thread Starter
Member
Re: Need advice with LINQ code
Thanks jmcilhinney, your suggestion worked great. Just out of curiosity, you said that I shouldn't use LINQ code. Can you give a suggestion when LINQ would be appropriate. I was always on the understanding that LINQ was designed to manipulate a database.
Again thank you,
-
Aug 9th, 2013, 10:08 AM
#4
Re: [RESOLVED] Need advice with LINQ code
LINQ is certainly not designed to manipulate a database. LINQ is designed to allow you to query lists in a consistent way that resembles the way you query a database using SQL. LINQ provides a common syntax but there are numerous LINQ providers that are each designed to query different types of lists. There is LINQ to Objects for enumerable collections, LINQ to DataSet for query DataTables, LINQ to XML for querying XML documents, LINQ to SQL for querying SQL Server databases and LINQ to Entities for querying Entity Framework models. LINQ providers can be created by anyone to provide access to any type of list.
In this particular case, if you were to use LINQ then you'd be using LINQ to DataSet to query your local DataSet. It's the DataSet and its associated table adapters that perform the interaction with the database. The thing is, it's simpler and more efficient to filter via a BindingSource in this particular case.
It's also worth noting that your LINQ query was actually taking the data out of the DataTable and populating new objects. As such, any changes you made would not affect the DataTable and would therefore not be easy to save back to the database.
-
Aug 9th, 2013, 10:20 AM
#5
Thread Starter
Member
Re: [RESOLVED] Need advice with LINQ code
Thanks jmcilhinney, that helps a lot. It gives me a better understanding of LINQ, and after reading your post, it makes perfect sense. I appreciate you taking the time to explain it.
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
|