Use reflection. I change the object names a bit and refactored the sample list, but you'll get the picture.
public class InboundDataItem
{
public string OriginCompany { get; set; }
public string OriginState { get; set; }
public string OriginCity { get; set; }
}
public class InboundDataList : List<InboundDataItem>
{
public bool HasItems { get { return this.Count > 0; } }
public InboundDataList()
{
this.Add(new InboundDataItem { OriginCompany = "Metro", OriginState = "NY", OriginCity = "New York" });
this.Add(new InboundDataItem { OriginCompany = "Costco", OriginState = "CA", OriginCity = "Eastvale" });
this.Add(new InboundDataItem { OriginCompany = "Walmart", OriginState = "OH", OriginCity = "Westwood" });
}
}
InboundDataList data = new InboundDataList();
string[] names = new string[]{"OriginCompany", "OriginState"};
if (data.HasItems)
{
PropertyInfo[] properties = typeof(InboundDataItem).GetProperties().Where(x=>(names.Contains(x.Name))).ToArray();
if (properties.Count() == names.Count())
{
foreach(InboundDataItem item in data)
{
foreach (PropertyInfo property in properties)
{
string value = property.GetValue(item).ToString();
Console.WriteLine(value);
}
}
}
}