Click here to Skip to main content
15,351,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
columnName          colGroup
-----------------   ---------------
OriginCompany	    YES
OriginAddress	    YES
OriginZip	        OK
OriginState	        NO

OriginCompany	OriginAddress	OriginZip   	OriginState
------------    ------------   ------------    ------------      
Company1	    PAdd2_1		    10001	        MI
Company1	    PAdd2_1		    10001	        MI	

Company2	    PAdd2_2		    10002	        MI

Company3	    PAdd2_3		    10003	        MI
Company3	    PAdd2_3		    10003	        MI

What I'm looking for is:
Step 1- From DataTable1, get columnName where colGroup is YES or OK

Step 2- Query DataTable2, and group by on columns returned from DataTable1 (Step1)

What I have tried:

IEnumerable<string> result = 
      (from d in dataTable1.AsEnumerable()
       d.Field<string>("colGroup") == "YES" ||
       d.Field<string>("colGroup") == "OK"
       select d.Field<string>("columnName")).ToList();

       var query = from s in dataTable2.AsEnumerable()
       .GroupBy(s => new { from q in result select q})
       select s;
Updated 15-Dec-21 11:34am

Maybe not the most efficient, but with few columns, shouldn't make much difference. - Remove columns from DataTable in C# - Stack Overflow[^]

So, remove any "not" in the selected list of columns.

It seems to me that working with DataTables is not easy as lots of casting is needed in order to extract anything more meaningful than an object from them. I would do something like this to get the required column names from table1.

//convert to an enumerable of rows
 var rows = table1.AsEnumerable();
 var colNames = rows.Where(r => (string)r["colGroup"] == "YES" || (string)r["colGroup"] == "OK")
     .Select(r => r.Field<string>("columnName"));

Now query table2 accessing the relevant Column by using the Row Indexer that takes a string to index into the Columns.

//instantiate a comparer, see last paragraph
 var comparer= new IEnumerableEqualityComparer<object>();
 var query = table2.AsEnumerable()
   .GroupBy(row =>; colNames.Select(name =>; row[name]), comparer)
     //select a value tuple
   .Select(group =>;(Keys:group.Key, Count :group.Count()));

You can enumerate the query like this
foreach (DataRow row in table2.Rows)
    foreach (DataColumn column in table2.Columns)

My generic comparer is too simplistic. Search for IEqualityComparer for SequenceEqual for some better examples

   public class IEnumerableEqualityComparer<T> : IEqualityComparer<IEnumerable<T>>
    public bool Equals(IEnumerable<T> x, IEnumerable<T> y)
        return  x.SequenceEqual(y);

    public int GetHashCode(IEnumerable<T> items)
        unchecked//allow overflow
            int hash = 17;
            foreach (var item in items)
                //multiplying by primes improves the distribution
                hash = hash * 31 + item.GetHashCode();
            return hash;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900