Solved

Using "ForEach"

Posted on 2009-05-04
11
974 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
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 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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

762 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