[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How can I get list items using LINQ?

Posted on 2009-05-01
13
Medium Priority
?
1,057 Views
Last Modified: 2013-11-11
i have a property like this:

public List<CreditCard> creditcardList
       {
           get
           {
               if (_creditCardList == null)
               {
                 return _creditCardList = new List<CreditCard>();
               }
               return _creditCardList;
           }
           set { _creditCardList = value; }
       }



I populate it like this:
basketPayment.creditcardList.Add(card);


I want to extract that "card" item from it using LINQ. How can I do this with LINQ? I tried creditcardlist.Single() but i get an error that
sequence has no elements.
0
Comment
Question by:Camillia
  • 6
  • 3
  • 3
  • +1
13 Comments
 
LVL 15

Expert Comment

by:mohan_sekar
ID: 24283281
for each objCard in creditcardlist
    objcard.<property name>
next
0
 
LVL 7

Author Comment

by:Camillia
ID: 24283294
is this LINQ?? i dont think so.
0
 
LVL 9

Accepted Solution

by:
tillgeffken earned 750 total points
ID: 24283304
CreditCard creditCard = basketPayment.creditCardList.First();
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 7

Author Comment

by:Camillia
ID: 24283326
ah, i did this. This works. "First"...what if I have more items ..First will get one the first one only..no??

var card1 = from p in basketPayment.creditcardList
                           select p;

                List<CreditCard> values = card1.ToList();
                Basket.PaymentMethod = values.Single();
0
 
LVL 12

Assisted Solution

by:GuitarRich
GuitarRich earned 450 total points
ID: 24283327
If you want a particular item and you have an Id or other identifying mark you could do filter on that or just get the first item

CreditCard cc = creditCardList.FirstOrDefault(ccl => ccl.PropertyName == propertyValue);
 
OR
 
CreditCard cc = creditCardList.FirstOrDefault();

Open in new window

0
 
LVL 9

Expert Comment

by:tillgeffken
ID: 24283391
Well, if you have more than one cards in the List and are not interested in a particular one but all of them why not just iterate through the list? If you're interested in a particular card you would probably want to store the cards in a different way.

List<KeyValuePair> comes to mind. Use a unique identifier for the key and the card object as value
0
 
LVL 7

Author Comment

by:Camillia
ID: 24283396
what if I want to loop thru all of list items with LINQ?? my method works but can I use it to loop thru??
0
 
LVL 15

Expert Comment

by:mohan_sekar
ID: 24283397
creditcardlist is a List, right? Don't you want to loop through your list and read all the items?
0
 
LVL 7

Author Comment

by:Camillia
ID: 24283404
How can I use "ForEach" for this? i think i'm getting at that...
0
 
LVL 15

Assisted Solution

by:mohan_sekar
mohan_sekar earned 300 total points
ID: 24283418
First, ensure that your list is not empty. Then, loop through to get all the items
0
 
LVL 9

Expert Comment

by:tillgeffken
ID: 24283419
basketPayment.creditcardList.ForEach(CreditCard creditCard)
{
    BillCreditCard(creditCard);
}
0
 
LVL 7

Author Comment

by:Camillia
ID: 24284090
thanks, let me try the ForEach. Will post back/
0
 
LVL 7

Author Comment

by:Camillia
ID: 24294571
no, this foreach loop doesnt work. I will close this question and open another one. thanks/.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

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 …
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

831 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