Solved

Using "ForEach"

Posted on 2009-05-04
11
970 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 7

Author Comment

by:Camillia
Comment Utility
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
Comment Utility
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
Comment Utility
(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
Comment Utility
you're right. Let me think about this. Thanks.
0
 
LVL 7

Author Comment

by:Camillia
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Paging GridView 7 32
.Net application crashing 6 19
Trouble connecting to SqlServer database 4 29
Setting runtime form location 4 18
Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 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

18 Experts available now in Live!

Get 1:1 Help Now