Solved

Rewriting this Foreach in LINQ

Posted on 2009-05-06
3
263 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

734 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