How can I use Linq with Dataset.xsd files?

How can I use Linq with Dataset.xsd files?

I've looked at Linq-to-Datasets and Linq-to-XSD, but they don't really work directly with Visual Studio DataSet.xsd files.

EDIT: I actually found a great link for this: link text , but I can't seem to figure out what to do if I want to query related tables.

    Dim taFields As New TestDSTableAdapters.FieldTableAdapter()
    Dim fields = taFields.GetData()

    Dim results = From f In fields Select f

    For Each field In results
        Response.Write(field.FieldID & " " & field.Label & " " & field.FieldTypeRow.FieldTypeLabel)
    Next

      

I am getting an object reference not set to an object instance. error when running the above code because I think field.FieldTypeRow.FieldTypeLabel is not actually part of the data request. Should I create a method that also returns data from this data? I guess this will be the case (that taFields.GetData should also return all FieldType data results if I'm going to reference it - in Linq to SQL it does all of this for me, so it's a bit frustrating)

0


source to share


2 answers


The DataSet is just a container for your data, so you need to fill it up first. LINQ to SQL will create SQL and go into the database for you ... but when you work with DataSets you are using LINQ to Objects , which won't create SQL. Therefore, you need to make sure that the all tables you need in the DataSet are full before you start writing LINQ to query your DataSet.

I think you are looking for something like this:



Dim taFields As New TestDSTableAdapters.FieldTableAdapter()
Dim taFieldTypes As New TestDSTableAdapters.FieldTypesTableAdapter()

Dim ds As New TestDS

taFields.Fill(ds.Fields)
taFieldTypes.Fill(ds.FieldTypes)

Dim results = From f In ds.Fields Select f

For Each field In results
    Response.Write( _
        field.FieldID & " " & field.Label & " " & 
            field.FieldTypeRow.FieldTypeLabel)
Next

      

+2


source


   Dim taFields As New TestDSTableAdapters.FieldTableAdapter()
    Dim fields as TestDSTableAdapters.FieldsDataTable = taFields.GetData()

    Dim results = From f In fields Select f

    For Each field In results
        Response.Write(field.FieldID & " " & field.Label & " " & field.FieldTypeRow.FieldTypeLabel)
    Next

      



You forgot to set the type for the fields. This is why you are having trouble referencing objects. You don't need to create a new empty dataset.

0


source







All Articles