@Zeeshan Dar , Based on your description, you want to get a group result by the condition of the step1.
According to my attempt, I suggest that you could use dynamic linq to Groupby a string array.(Need to install nuget-package System.Linq.Dynamic)
Here is a code example you could refer to.
using System.Linq.Dynamic;
static void Main(string[] args)
{
DataTable table1=new DataTable();
table1.Columns.Add("columnName", typeof(string));
table1.Columns.Add("colGroup", typeof(string));
table1.Rows.Add("OriginCompany", "Yes");
table1.Rows.Add("OriginAddress", "Yes");
table1.Rows.Add("OriginZip", "OK");
table1.Rows.Add("OriginState", "NO");
var result = table1.AsEnumerable().Where(i => i.Field<string>("colGroup")=="Yes"||i.Field<string>("colGroup")=="Ok").Select(i => i.Field<string>("columnName")).ToList();
DataTable table2 = new DataTable();
foreach (DataRow item in table1.Rows)
{
table2.Columns.Add(item[0].ToString());
}
table2.Rows.Add("Company1", "PAdd2_1", 1001, "MI");
table2.Rows.Add("Company1", "PAdd2_1", 1001, "MI");
table2.Rows.Add("Company2", "PAdd2_2", 1002, "MI");
table2.Rows.Add("Company3", "PAdd2_3", 1003, "MI");
table2.Rows.Add("Company3", "PAdd2_3", 1003, "MI");
table2.Rows.Add("Company1", "PAdd2_1", 10019, "MI");
string field = string.Format("new({0})", string.Join(",", result));
var list1 = (from DataRow dr in table2.Rows
select new
{
OriginCompany = dr["OriginCompany"].ToString(),
OriginAddress = dr["OriginAddress"].ToString(),
OriginZip =Convert.ToInt32(dr["OriginZip"].ToString()),
OriginState = dr["OriginState"].ToString()
}).ToList();
var result1 = list1.GroupBy(field, "it");
foreach (IGrouping<dynamic, dynamic> item in result1)
{
foreach (var i in item)
{
Console.WriteLine(i.OriginCompany);
Console.WriteLine(i.OriginAddress);
Console.WriteLine(i.OriginZip);
Console.WriteLine(i.OriginState);
}
Console.WriteLine("********");
}
Console.ReadKey();
}
Result:(I add another test data to test the code)
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.