Solved

Rewriting this Foreach in LINQ

Posted on 2009-05-06
3
260 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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 …
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
A short film showing how OnPage and Connectwise integration works.

929 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

8 Experts available now in Live!

Get 1:1 Help Now