Search code examples
strongly-typed-datasetlinq

LINQ - SelectMany with Strongly-typed DataSets


I have the following query that returns exactly what I need,

var dataRows = 
            (from headerLocationRow in headerLocationDataTable
             select WellsDao.Instance.GetAllWellData(headerLocationRow.HEADER_ID).WELL_BORE_CONSOLIDATED)
             .SelectMany(x => x.Select());

but I don't like how it mixes the inline query with extension methods. This is for an older project, so I'm stuck with strongly-typed DataSets. I tried to use two from statements, but it didn't like that. headerLocationDataTable is a strongly-typed DataTable. That WellsDao.Instance.Get... nonsense traverses the DataSet and returns a collection of DataTables strongly-typed as WELL_BORE_CONSOLIDATED based on the HEADER_ID field in the headerLocationDataTable.

This isn't a huge deal because the query works, but I'm really trying to get a handle on LINQ, so I just want to know how to do this whole thing as inline. Or if you know of a more elegant way of writing this, please share. Ultimately, I want to get back a flat list of DataRows that contains all of the WELL_BORE_CONSOLIDATED rows, regardless of which parent headerLocationRow they are associated with.


Solution

  • This should do what you want:

    var dataRows =  from headerLocationRow in headerLocationDataTable
                    from wbcRow in WellsDao.Instance.GetAllWellData(headerLocationRow.HEADER_ID).WELL_BORE_CONSOLIDATED
                    select wbcRow;
    

    It's the query syntax of a SelectMany.