?
Solved

Using "ForEach"

Posted on 2009-05-04
11
Medium Priority
?
977 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
Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

 
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 2000 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

719 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