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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Hardik DhankechaPosted Sep 11, 2021, 1:57 PM
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...!!!
Sachin SinghPosted Sep 10, 2021, 4:00 AM
Abhishek ChadhaPosted Sep 9, 2021, 5:30 PM
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
Shweta LodhaPosted Sep 9, 2021, 5:19 PM