• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 207
  • Last Modified:

How do I change the text on an object property of a generic list of objects?

If Ive got a generic list of objects and I want to change the text on one of those objects properties, how do I do it?
 
Gary Davis
class Prod
        {
            public Prod()
            {
            }
 
            public int Id { get; set; }
            public string Title { get; set; }
        }
 
            var prd = new List<Prod>();
            var prd1 = new Prod {Id = 1, Title = "MY TITLE"};
            var prd2 = new Prod { Id = 2, Title = "SOME THING" };
            prd.Add(prd1);
            prd.Add(prd2);
 
  //this doesn't work - i assume because the lambda is trying to change the object, not the property
 
            prd.ForEach(x => x.Title.ToLower()); 
 
            foreach (var p in prd)
                Console.WriteLine(string.Format("id={0} title={1}", p.Id, p.Title));

Open in new window

0
Gary Davis
Asked:
Gary Davis
  • 4
  • 2
1 Solution
 
p_davisCommented:
try

 prd.ForEach(delegate(Prod p1)
            {
                p1.Title = p1.Title.ToLower();
               
            });
0
 
p_davisCommented:
or if you wish to stick to the lambda

 prd.ForEach(x =>x.Title = x.Title.ToLower());
0
 
p_davisCommented:
basically it wasn't modifying it because ToLower() returns a string it doesn't modify the string that it is referencing.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
käµfm³d 👽Commented:
Change line 10 to:
prd.ForEach(x => x.Title = x.Title.ToLower()); 

Open in new window

0
 
käµfm³d 👽Commented:
Curses! Foiled by p_davis again  :)

Couldn't get my test app built fast enough, lol!
0
 
p_davisCommented:
happens to me all the time.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now