Results 1 to 5 of 5

Thread: [RESOLVED] Need advice with LINQ code

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2009
    Location
    Ontario, Canada
    Posts
    54

    Resolved [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
    .ProductElectrical.NameElectrical.PictureElectrical.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.

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

    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'"

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2009
    Location
    Ontario, Canada
    Posts
    54

    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,

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

    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.

  5. #5

    Thread Starter
    Member
    Join Date
    Dec 2009
    Location
    Ontario, Canada
    Posts
    54

    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
  •  



Click Here to Expand Forum to Full Width