For Each loop requiring 2 field combination

I need to loop in my repeater control which has the following header..
Unit Id

I need something like For Each UnitID and Type(This is the combination)
Who is Participating?
Fernando SotoConnect With a Mentor RetiredCommented:
Hi zachvaldez;

Let assume that the class that holds the data is named Unit and that you have all the data in a List collection then you can do what you need as follows.
// Test Data
List<Unit> unitInfo = new List<Unit>() {new Unit{UnitId = 1, Type = "A1", Street = "1 Main St", City = "New York", State = "NY", Zip = "10001"},
		new Unit{UnitId = 2, Type = "B2", Street = "21 York Ave", City = "Boston", State = "MA", Zip = "45601"},
		new Unit{UnitId = 1, Type = "A1", Street = "11 John St", City = "New York", State = "NY", Zip = "10001"}};

// Linq Query to get all matching records into groups		
var unitid_type = (from u in unitInfo
				   group u by new {Unit=u.UnitId, Type=u.Type} into unitGroup
				   select new {
				     UnitType = unitGroup.Key,
				     Units = unitGroup.ToList()

// Get the records for each group.				  
foreach (var unit in unitid_type)
	Console.WriteLine("UnitID/Type {0}/{1}", unit.UnitType.Unit, unit.UnitType.Type);
	foreach (Unit u in unit.Units)
		Console.WriteLine("\t{0}\t{1}\t{2}\t{3}", u.Street, u.State, u.City, u.Zip);

// Test class
public class Unit
	public int UnitId    {get; set;}
	public string Type   {get; set;}
	public string Street {get; set;}
	public string City   {get; set;}
	public string State  {get; set;}
	public string Zip    {get; set;} 

Open in new window

Results for the above snippet.
UnitID/Type 1/A1
  1 Main St  NY  New York  10001
  11 John St  NY  New York  10001
UnitID/Type 2/B2
  21 York Ave  MA  Boston  45601

Open in new window

May be I not correct understand you..
Do you want to get value of all properties in your class?
In this case you can use "reflection"
YourClassType cla = new YourClassType {Unit Id  = 1,Type  = "qwe"};
foreach(var prop in cla.GetType().GetProperties()) {
    Console.WriteLine("{0}={1}", prop.Name, prop.GetValue(cla, null));

Open in new window

Fernando SotoRetiredCommented:
What do you mean by this, "Type(This is the combination) "??
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

zachvaldezAuthor Commented:
----What do you mean by this, "Type(This is the combination) "??
Sorry if I wasn't clear.  It's saying like
" If unitID and type are not null or empty...."

 I'm proceeding only if unitID and type have values...
YourClassType cla = new YourClassType {Unit_Id  = 1,Type  = "qwe"};
if (cla.GetType().GetProperty("Unit_Id").GetValue(cla) != null && cla.GetType().GetProperty("Type").GetValue(cla) != null)
//Do someting

Open in new window

is it the fact that you wanted?
Fernando SotoRetiredCommented:
Hi zachvaldez, I see that you marked my solution As Helpful. If it answered your question please close by awarding points.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.