Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Rewriting this Foreach in LINQ

Posted on 2009-05-06
3
Medium Priority
?
266 Views
Last Modified: 2013-11-11
This works but i like to know how I can write in in LINQ. Even tho i have "for each", I only loop thru it once to find the ID that's equal and do whatever.

I have "break" just because i dont see it necessary to keep looping once i find my value.
basketpaymentList = ViewState["BasketPayment"] as List<.BasketPayment>;
 
 foreach (BasketPayment p in basketpaymentList)
                {
                    if (p.id == new Guid(id))
                    {
                      
                        basketpaymentList.Remove(p);
                        break;  
                    }
                }

Open in new window

0
Comment
Question by:Camillia
3 Comments
 
LVL 12

Accepted Solution

by:
GuitarRich earned 1800 total points
ID: 24316481
Try this:
BasketPayment bp = basketpaymentList.FirstOrDefault(b => b.id = new Guid(id));
basketpaymentList.Remove(bp);
0
 
LVL 21

Assisted Solution

by:naspinski
naspinski earned 200 total points
ID: 24316510
I think you need to understand how Linq really works, we already explained a question almost exactly like this.  If you just keep getting solutoins, you will never understand how to get these on your own.  Google some linq tutorials and take some time to learn how Linq works - it will be much better time spent then asking every speed bump on here.

Don't get me wrong, we are here to help, but this is almost *exactly* like your last question, so that tells me you aren't learning anything.
0
 
LVL 7

Author Comment

by:Camillia
ID: 24316536
naspinski - you're right.
0

Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Screencast - Getting to Know the Pipeline
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

916 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