Solved

Using "ForEach"

Posted on 2009-05-04
11
971 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
  • 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
 
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
DevOps Toolchain Recommendations

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

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

911 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

22 Experts available now in Live!

Get 1:1 Help Now