Let say I got column names:
IList<string> selectedColumn = new List<string>{"Name", "City", "CreatedAt"};
Into loop from some entries I'm taking data:
foreach (Car car in rowsWithAllCar)
{
string name = car.Name;
string lastName = car.LastName;
string city = car.City;
string home = car.Home;
DateTime createdAt= (DateTime)car.CreatedAt;
string[] allItems = {name, lastName, phone, city, createdAt}
}
How to check if for example value car.LastName
or car.Home
is not in selectedColumn
? As I don't want to add this to my allItems
.
Result should be:
string[] allItems = {name, city, createdAt};
CodePudding user response:
Is this what you want to do?
IList<string> selectedColumn = new List<string> { "Name", "City", "CreatedAt" };
foreach (Car car in rowsWithAllCar)
{
var properties = car.GetType().GetProperties();
var allItems = properties
.Where(p => selectedColumn.Contains(p.Name))
.Select(p => p.GetValue(car))
.Cast<string>()
.ToArray();
//string name = car.Name;
//string lastName = car.LastName;
//string city = car.City;
//string home = car.Home;
//DateTime createdAt = (DateTime)car.CreatedAt;
//string[] allItems = { name, lastName, phone, city, createdAt };
}
I commented your code.
CodePudding user response:
Do you mean something like this?
List<string> compareString = new List<string>();
compareString = selectedColumn.Except(allItems);