Solved

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

Posted on 2009-04-10
6
192 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

911 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

20 Experts available now in Live!

Get 1:1 Help Now