Solved

Rewriting this Foreach in LINQ

Posted on 2009-05-06
3
262 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 450 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 50 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

820 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