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



DataTable2
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:

C#
IEnumerable<string> result = 
      (from d in dataTable1.AsEnumerable()
       where
       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;
Posted
Updated 15-Dec-21 11:34am

Maybe not the most efficient, but with few columns, shouldn't make much difference.

asp.net - Remove columns from DataTable in C# - Stack Overflow[^]

So, remove any "not" in the selected list of columns.
 
Share this answer
 
v2

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.


C#
//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.


C#
//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
C#
foreach (DataRow row in table2.Rows)
{
    foreach (DataColumn column in table2.Columns)
    {
        Console.WriteLine(row[column]);
    }
}

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


C#
   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;
        }
    }
}
 
Share this answer
 
v2

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