Ashu
Ashu

Reputation: 23

Linq Query for querying a list of DataSet

I have a list of DataSet.

for example:

List<DataSet> list = new List<DataSet>();

For my task, the number of DataSet in the list and the number of DataTable in each DataSet will be known at the run time. Now I want to get those tables from the DataSets that contains a certain string in their names, for instance say 'Group1'.

I am trying with the following code:

var ds= from set in list from table in set  
        where li.Where(e=>e.Tables.Contains("Group")) select table;

But i am getting the error as 'An expression of type System.Data.DataSet is not allowed in a subsequent from clause in a query expression with source typeList'.

Please help me with the correct approach.

Upvotes: 0

Views: 296

Answers (3)

Najera
Najera

Reputation: 2879

This gets the tables with the contained name:

 var tables = list.SelectMany(x => x.Tables.Cast<DataTable>())
     .Where(x => x.TableName.Contains("Group"));

Upvotes: 0

tinstaafl
tinstaafl

Reputation: 6948

You original query is close. It just needs to be fleshed out a bit. First off it helps to declare the type in the from statement. Also specify you want the table collection from the set. The where clause should just need to examine the TableName property of the tables:

List<DataSet> list = new List<DataSet>();
var ds = from DataSet set in list
         from DataTable table in set.Tables
         where table.TableName.Contains("Group")
         select table;

Upvotes: 0

FatAdama
FatAdama

Reputation: 91

I've tried to replicate your data structure by creating another class. Hope this helps.

  namespace TestCode
    {
        class Program
        {
            static void Main(string[] args)
            {
                var list = new List<TC> {new TC(2), new TC(2), new TC(3), new TC(4), new TC(5), new TC(2)};

                var dt = list.Where( // Contains 3 elements
                    x => x.X == 2
                    );

                //var ds = from set in list
                //         from table in set
                //         where li.Where(e => e.Tables.Contains("Group"))
                //         select table;
            }
        }

        internal class TC
        {
            public int X { get; set; }
            internal TC(int val)
            {
                X = val; 
            }
        }

    }

Upvotes: 1

Related Questions