假设我得到了列名:
IList<string> selectedColumn = new List<string>{"Name", "City", "CreatedAt"};
从一些条目进入循环,我正在获取数据:
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}
}
如何检查值car.LastName
或car.Home
是否不在selectedColumn
中?因为我不想把它添加到我的allItems
中。
结果应该是:
string[] allItems = {name, city, createdAt};
你的意思是这样的吗?
List<string> compareString = new List<string>();
compareString = selectedColumn.Except(allItems);
这就是我解决它的方法:
foreach (Car car in rowsWithAllCar)
{
IDictionary<string, string> carFields = new Dictionary<string ,string>();
string name = car.Name;
carFields.Add("Name", name);
string lastName = car.LastName;
carFields.Add("LastName", lastName);
string city = car.City;
carFields.Add("City", city);
...
IList<string> allItems = new List<string>();
foreach (var carField in carFields)
{
bool isInSelectedColumn = selectedColumn.Contains(carField.Key);
if (isInSelectedColumn)
{
allItems.Add(carField.Value);
}
}
//allItems.ToArray();
}