For Each loop requiring 2 field combination

I need to loop in my repeater control which has the following header..
Unit Id
Type
Street
City
State
Zip

I need something like For Each UnitID and Type(This is the combination)
zachvaldezAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MishaProgrammerCommented:
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

0
Fernando SotoRetiredCommented:
What do you mean by this, "Type(This is the combination) "??
0
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...
0
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference ithrough the Colors of STEM program.

MishaProgrammerCommented:
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?
0
Fernando SotoRetiredCommented:
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

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fernando SotoRetiredCommented:
Hi zachvaldez, I see that you marked my solution As Helpful. If it answered your question please close by awarding points.

Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.