Rewriting this Foreach in LINQ

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 ( == new Guid(id))

Open in new window

Who is Participating?
GuitarRichConnect With a Mentor Commented:
Try this:
BasketPayment bp = basketpaymentList.FirstOrDefault(b => = new Guid(id));
naspinskiConnect With a Mentor Commented:
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.
CamilliaAuthor Commented:
naspinski - you're right.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.