Solved

Using "ForEach"

Posted on 2009-05-04
11
976 Views
Last Modified: 2013-11-11
This works but because of ".single()", I only get one value. How can I get all the values?

var card = from p in basketPayment.creditcardList
                       select p;
            List<CreditCard> values = card.ToList();
            Basket.PaymentMethod = values.Single(); //***brings back ONE value of PaymentMethod type

--- I tired this. No errors but this has to bring back a type of PaymentMethod:
  values.ForEach(delegate(CreditCard test) { CreditCardType p = test.CardType; });
   
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
  • 6
  • 5
11 Comments
 
LVL 39

Expert Comment

by:abel
ID: 24294892
How do you declare the PaymentMethod property/object of the Basket variable? is it a list, or is it a string? Do you want the values concatenated by, for instance, commas, or do you want them in some array or IList<yourType>?
0
 
LVL 7

Author Comment

by:Camillia
ID: 24295061
i can have them in an array list of IList<yourtype>.

PaymentMethod looks like this;

public PaymentMethod PaymentMethod
        {
            get
            {
                if (_paymentMethod == null)
                {
                    if (Customer != null)
                    {
                        CustomerPaymentMethod defaultMethod = Customer.DefaultPaymentMethod;
 
                        if (defaultMethod != null)
                        {
                            _paymentMethod = defaultMethod.PaymentMethod;
                        }
                    }
                }
 
                return _paymentMethod;
            }
            set
            {
                _paymentMethod = value;
            }
        }

Open in new window

0
 
LVL 7

Author Comment

by:Camillia
ID: 24295084
Another example of basketPayment. It looks like this. I want to save "card"

CreditCard card = CreditCard.NewCreditCard(txtCardNumber.Text.Trim());
card.SecurityCode = txtSecurityCode.Text.Trim();
Basket.PaymentMethod = card;
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 39

Expert Comment

by:abel
ID: 24295213
hmm, I see a "PaymentMethod" above that is apparently equal to CustomerPaymentMethod, and in the last code example, you show a CreditCard payment method. Both show a way of assigning one value to the property. If the property can only take one value, how do you expect to give it multiple values? First and foremost, we'll have to change the property to be become a List<PaymentMethod> or something similar (like an array, an associated list like Dictionary or NameValue list), depending on the rest of your app.
0
 
LVL 7

Author Comment

by:Camillia
ID: 24295267
let me think about this. I will keep the question open for now. I think you are correct that the property has to change to a list. But let me think. Thanks
0
 
LVL 7

Author Comment

by:Camillia
ID: 24295306
question: how about if i decided to have a comma delimited list..then i dont have to change the property..right?
0
 
LVL 39

Expert Comment

by:abel
ID: 24295428
no, not right. A comma delimited list is either an object that contains a comma delimited list (in any which way), or it is a string property (and yours isn't) that consists of a comma delimited list. So, in that case, you would still have to change the property type.
0
 
LVL 39

Expert Comment

by:abel
ID: 24295440
(sorry about that, but in general, changing any piece of code from one-containing to many-containing requires quite some refactoring throughout the code)
0
 
LVL 7

Author Comment

by:Camillia
ID: 24295774
you're right. Let me think about this. Thanks.
0
 
LVL 7

Author Comment

by:Camillia
ID: 24295785
Still, suppose that property is a list, how can use ForEach with that code??
0
 
LVL 39

Accepted Solution

by:
abel earned 500 total points
ID: 24295924
You could still use for-each, with .Add(elem) if you want, but that would hold the same as just using .AddRange(elems), which will add all elements at once to the list. In your case, but without testing because I'm not sure of all the datatypes you are using, I would do something like this:

Basket.PaymentMethods.AddRange(card.ToList());
If you want to use for-each, you use up a bit more lines, and it will look like this:

foreach (CreditCard method in card.ToList()){    Basket.PaymentMethods.Add(method);}
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

623 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