none
Loop thru List<T> and access properties RRS feed

  • Question

  • Hi, 

    In the example below I am unable to access the property u.Product because the method received  a List<T>.  What do I needto do in order that I can access the properties Product & Total? 

    This is just an example, in the real world the equivalent ProcessComponents method would handle different types of List<T>

    Thanks. 

    void Main()
    {
    	var updates = new List<Component>();
    	updates.Add(new Component() {Product="widget1", Total=2});
    	updates.Add(new Component() {Product="widget2", Total=4});
    	updates.Add(new Component() {Product="widget3", Total=6});
    	ProcessComponents<Component>(updates);	
    }
    
    void ProcessComponents<T>(List<T> updates)
    {
    	Console.WriteLine (updates.Count);
    	
    	foreach (var u in updates)
    	{
    //		Console.WriteLine (u.Product);
    	}
    }
    
    public class Component
    {
    	public string Product { get; set; }
    	public int Total { get; set; }
    }



    Thursday, November 9, 2017 2:15 PM

Answers

  • You will have to use reflection to update the value or get the existing value which would be like:

    void ProcessComponents<T>(List<T> updates)
    {
    
        Console.WriteLine (updates.Count);
    
        PropertyInfo property = GetProperty("Product");
    
        foreach (T item in updates)
        {
           Console.WriteLine(property.GetValue(item));
        }   
    	
    }
    Hope it helps!



    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, November 9, 2017 3:05 PM
  • Ah ok, this actually worked as well. 

    foreach (var u in updates as List<Component>)
    {
    	Console.WriteLine (u.Product);
    	Console.WriteLine (u.Total);
    }

    Thursday, November 9, 2017 4:18 PM

All replies

  • You will have to use reflection to update the value or get the existing value which would be like:

    void ProcessComponents<T>(List<T> updates)
    {
    
        Console.WriteLine (updates.Count);
    
        PropertyInfo property = GetProperty("Product");
    
        foreach (T item in updates)
        {
           Console.WriteLine(property.GetValue(item));
        }   
    	
    }
    Hope it helps!



    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, November 9, 2017 3:05 PM
  • Ah ok, this actually worked as well. 

    foreach (var u in updates as List<Component>)
    {
    	Console.WriteLine (u.Product);
    	Console.WriteLine (u.Total);
    }

    Thursday, November 9, 2017 4:18 PM
  • yes that would work, but it would not work if on calling side we use different class as type parameter for example, let's say SubComponent, it will still try to cast it to List<Component> using as operator

    If it is always expected to have List<Component>  to be passed to the method, then you can make it more simple by removing the generic type parameter  form :

    ProcessComponents<T>(List<T> updates)

    to:

    ProcessComponents(List<Compnoent> updates)

    and now you can simply do that, you whole method would look like now:

    void ProcessComponents(List<Component> updates)
    {
       foreach (var u in updates)
       {
    	Console.WriteLine (u.Product);
    	Console.WriteLine (u.Total);
       }
    }

    Thanks


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites




    Thursday, November 9, 2017 5:15 PM
  • If you believe that the generic T will always contain a Product property, then try this too:

       foreach( dynamic u in updates )

       {

          Console.WriteLine( u.Product );

       }

    Thursday, November 9, 2017 7:19 PM