?
Solved

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

Posted on 2009-04-10
6
Medium Priority
?
199 Views
Last Modified: 2012-05-06
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
Comment
Question by:Gary Davis
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 24117225
try

 prd.ForEach(delegate(Prod p1)
            {
                p1.Title = p1.Title.ToLower();
               
            });
0
 
LVL 22

Accepted Solution

by:
p_davis earned 2000 total points
ID: 24117233
or if you wish to stick to the lambda

 prd.ForEach(x =>x.Title = x.Title.ToLower());
0
 
LVL 22

Expert Comment

by:p_davis
ID: 24117241
basically it wasn't modifying it because ToLower() returns a string it doesn't modify the string that it is referencing.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 75

Expert Comment

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

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24117282
Curses! Foiled by p_davis again  :)

Couldn't get my test app built fast enough, lol!
0
 
LVL 22

Expert Comment

by:p_davis
ID: 24117292
happens to me all the time.
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question