Iterate over record in generic list?

Foreach(var element in MyList) { foreach(var property in element) { //do your thing } } But you were only selecting the First element on MyList.

Now I am running into a compile issue stating that myList doesn't contain a public definition for GetEnumerator and I cant simply cast the list to IEnumerable – Jake Oct 19 at 16:44.

Var myItem = MyList. Where(d=>d. Field).First(); Now that's just an item, not an IEnumerable.

What type is the item? Is it just some class with properties? If you want to loop through those you'd need to use reflection.

Sorry I wasn't clear but myItem returns an index within that list. Which that index is a list itself of records and that what I want to loop though. The item is a class with properties – Jake Oct 19 at 16:19 What are the properties of that class?

Is there a property that contains the sub-list? – Daniel Earwicker Oct 19 at 16:20 1 @Jake I suggest that you draw a nice ASCII picture of your data structure and post it to your question because it's difficult to follow your question. What do you mean by Which that index is a list itself of records?

– Icarus Oct 19 at 16:21 imagine a gridview and I am trying to update that record/row of data in a List based on the edit in the gridview – Jake Oct 19 at 16:23 @Jake - okay, I'm imagining that, and it's working perfectly. What's the problem? – Daniel Earwicker Oct 19 at 16:27.

Unless I'm misunderstanding this is your scenario list nested within a list. You are selecting one item in the parten list using some criteria and wish to set a property on all of the sub items of that item in the main list. If so the following maps you your scenario class B { public int x; } class A { public int b; public List lst = new List(); } static void Main(string args) { List Sub = new List() { new B { x = 2 }, new B { x = 3 } }; List Sub2 = new List() { new B { x = 4 }, new B { x = 6 } }; List Main = new List() { new A() {b =2, lst = Sub2 }, new A() {b=3 , lst = Sub } }; var newlst = Main.

Where((x) => x. B == 2).First().lst. Select((y)=> {y.

X = 5;return y;}); foreach (var item in newlst) { Console. WriteLine("A. Lst(x).

X = {0}", item. X); } }.

Well your list is set up like a linked list, but mine is essentially a dataset that represents a list, so imagine a DB table that I converted to a list and I want to iterate through one row of that table comparing column names, but on a list – Jake Oct 19 at 16:40 List is an indexable item not a linked list. So does items in list A contain ID to List B Which is also a list. – rerun Oct 19 at 16:45 sorry the list is IQueryable objects – Jake Oct 19 at 17:56.

This was able to do it: var mylist =(MYOBJECT)(MyList. Where(d=>d. Field).SingleorDefault()); PropertyInfo myproperties = mylist.GetType().GetProperties(); foreach( var record in myproperties) { if(record.Name==target) //target is the passed string }.

Seems less confusing. – Daniel Earwicker Oct 19 at 19:36 Refer to the documentation of System.Type. GetProperty which is currently at msdn.microsoft.

Com/en-us/library/kz0a8sxy. Aspx - it retrieves a property by name, avoiding the need to loop manually. You must then use property.

GetValue or property. SetValue to get or set the property value. – Daniel Earwicker Oct 19 at 19:36.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions