Click here to Skip to main content
15,900,725 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Seed Data:

C#
public class InboundLayoutNameValuePair
    {
        public string columnMappedTo { get; set; }
    }

public class InboundDataTable
    {
        public string OriginCompany { get; set; }
        public string OriginState { get; set; }
        public string OriginCity { get; set; }
    }

List<InboundLayoutNameValuePair> inboundLayoutNameValuePair = new List<InboundLayoutNameValuePair>  
{  
    new InboundLayoutNameValuePair { columnMappedTo = "OriginCompany" },  
    new InboundLayoutNameValuePair { columnMappedTo = "OriginState"},  
    new InboundLayoutNameValuePair { columnMappedTo = "OriginCity" }  
};  

List<InboundDataTable> inboundDataTable = new List<InboundDataTable>  
{  
    new InboundDataTable { OriginCompany = "Metro", OriginState = "NY", OriginCity = "New York" },  
    new InboundDataTable { OriginCompany = "Costco", OriginState = "CA", OriginCity = "Eastvale" },  
    new InboundDataTable { OriginCompany = "Walmart", OriginState = "OH", OriginCity = "Westwood" }
};



Using the dynamically selecting column from first list, I am looking to get the value of each column by index (and not by using the column name). Something like:

C#
for(int i = 0; i <= column.count(); i++)
     {
        //print column[i] value
     }


What I have tried:

ViewModel:

C#
@model Web.ViewModels.ValidateDataViewModel
@using System.Linq.Dynamic.Core;

var qryInboundColumns = Model.inboundLayoutNameValuePair.OrderBy(c => c.columnSerial);

    var joinColumns = String.Format("new ({0})", String.Join(",", qryInboundColumns.Select(c => c.columnMappedTo)));

    var excelData = Model.inboundDataTable.AsQueryable().Select(joinColumns).ToDynamicArray();

    foreach (var row in excelData)
    {
       <p>@row</p>
    }


It returns:
C#
Row1: { OriginCompany = Metropolitan, OriginState = NY, ..... }
Row2: { OriginCompany = Walmart, OriginState = CA, ..... }
Row3: { OriginCompany = Costco, OriginState = MA, ..... }


I only want to display the values in html table.
Posted
Updated 20-Dec-21 22:16pm

1 solution

Use reflection. I change the object names a bit and refactored the sample list, but you'll get the picture.

C#
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" });
    }
}

C#
// create our sample list of items
InboundDataList data = new InboundDataList();
// define the property names we want to do something with
string[] names = new string[]{"OriginCompany", "OriginState"};

// if the list has items
if (data.HasItems)
{
    // retrieve an array of properties with the specified names
    PropertyInfo[] properties = typeof(InboundDataItem).GetProperties().Where(x=>(names.Contains(x.Name))).ToArray();

    // if we have the same number of properties as desired names
    if (properties.Count() == names.Count())
    {
        // iterate the data
        foreach(InboundDataItem item in data)
        {
            // and for each item, iterate the property array
            foreach (PropertyInfo property in properties)
            {
                // get the value
                string value = property.GetValue(item).ToString();
                // do something with the value
                Console.WriteLine(value);
            }
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900