Solved

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

Posted on 2009-04-10
6
191 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
  • 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 500 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 Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 74

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 74

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now