I Have a multiple lists and wants to print all the columns values using the same function but both lists have different column names so is there way I can print column values without mentioning column names in c#
Loading
I Have a multiple lists and wants to print all the columns values using the same function but both lists have different column names so is there way I can print column values without mentioning column names in c#
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Sachin SinghRank #66 · 26.2K reputationPosted Sep 10, 2021, 12:00 AM EDT
Shweta LodhaRank #31 · 39.8K reputationPosted Sep 9, 2021, 1:19 PM EDT
Abhishek ChadhaRank #402 · 6.2K reputationPosted Sep 9, 2021, 1:30 PM EDT
currently i am printing my list -
this is my parameter holding list data - List allProducts
foreach (var emp in allproducts)
{
sb.Append(Environment.NewLine);
sb.AppendFormat("{0},{1},{2}",emp.id,emp.name,emp.dept);
}
foreach (var products in allProducts)
{
sb.Append(Environment.NewLine);
sb.AppendFormat("{0},{1},{2}",products.id,products.name,products.color);
}
Now instead of making 2 different loops i want to print both the emp and product data using same loop
Hardik DhankechaRank #9,518 · 205 reputationPosted Sep 11, 2021, 9:57 AM EDT
Hi Abhishek,
As per your code explanation in comments, it's showing as both lists are different. So if both lists are different and not mapped with each other, then you need to use the for loop twice there. But if both lists are mapped and related to each other, then you need to perform the for loop twice, but this time a nested list for loop will run under the parent list.
Like if the parent list is List allCustomer and the nested list is a product which is mapped with customer, then your second list will be List allProducts. And the next loop will be like this.
foreach(var item in allCustomer)
{
sb.Append(Environment.NewLine);
sb.AppendFormat("{0},{1},{2}",emp.id,emp.name,emp.dept);
//If product list is under customer, then you can use below for loop directly, but if product list is not under the customer, but productList contains customer id or mapping, then you can fetch selected items from allProducts list.
//Like : allProducts.where(x => x.FkCustomerId == item.CustomerId).toList();
foreach(var subItem in item.products)
{
sb.Append(Environment.NewLine);
sb.AppendFormat("{0},{1},{2}",products.id,products.name,products.color);
}
}
Happy Coding...!!!