Query a DataSet

2019-05-28 10:59发布

问题:

I'm reading data from XML files into a strong typed DataSet. The data ends up in multiple tables; can I run queries against it to create a denormalized view to display in a DataGrid?

Sample input:

<PeopleFile>
    <address>
        <street>123 Some Street</street>
        <town>Anytown</town>
        <resident>
            <first>Jane</first>
            <last>Doe</last>
        </resident>
        <resident>
            <first>John</first>
            <last>Doe</last>
        </resident>
    </address>
    <address>
        <street>456 Tree Street</street>
        <town>Westwood</town>
        <resident>
            <first>Mary</first>
            <last>Jones-Smith</last>
        </resident>
        <resident>
            <first>Mike</first>
            <last>Smith</last>
        </resident>
        <resident>
            <first>Kate</first>
            <last>Smith</last>
        </resident>
    </address>
</PeopleFile>

Desired output:

123 Some Street Anytown     Jane    Doe  
123 Some Street Anytown     John    Doe  
456 Tree Street Westwood    Mary    Jones-Smith  
456 Tree Street Westwood    Mike    Smith  
456 Tree Street Westwood    Kate    Smith  

EDIT: I should add that in addition to multiple tables per file, my real data is also split among multiple files which AFAIK will require being loaded into separate DataSets.

回答1:

Yes, use Linq. There is a special set of extensions called Linq-to-Datasets.

You will need .NET 3.5 obviously, and add using System.Data;

If your multiple files follow the same schema you should be able to read them into separate instances of the TypedDataSet and Merge() those instances.