Solved

Rewriting this Foreach in LINQ

Posted on 2009-05-06
3
259 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
Comment Utility
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
Comment Utility
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
Comment Utility
naspinski - you're right.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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 …
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

762 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

11 Experts available now in Live!

Get 1:1 Help Now