stuart-d2
5/27/2015 - 9:57 PM

Anonymous Type with a Collection As A Property

Anonymous Type with a Collection As A Property

/*This is a demo of anonymous type "request" that uses standalone Properties
AND a Property which is a collection type, "Settings".  
Also demo'd is a foreach statement, for iterating through a Anonymous Type's collection Property.  
*/
class Program {

static void Main()
{
//Anonymous Object that also a list object as a property... Has a list object as a property.
var request = new { Id = 1, UserId = 1, Settings = new List<IdValuePair>() { new IdValuePair { Id = 5, Value = "5" } } };

Console.WriteLine(request);
Console.WriteLine("request.Id : {0}", request.Id);
Console.WriteLine("request.UserId : {0}", request.UserId);
Console.WriteLine("request.Settings : {0}", request.Settings);

//foreeach ( <<collectionDataType>> <<tempName>> in <<objectCollectionProperty>>
foreach ( IdValuePair item in request.Settings)
{
	Console.WriteLine(item);  
}

}


public class IdValuePair 
{
public int Id { get; set; }
public string Value { get; set; }
}



// Define other methods and classes here
}