SolvedPrivate

I know the error - Adding to list only takes the last item

Posted on 2014-11-22
2
56 Views
Last Modified: 2016-02-17
I know what's causing this. I have "new" so with each loop, I create a new list. But don't know how to fix this.

var model = new MyViewModel();
            DataClasses1DataContext db = new DataClasses1DataContext();

            var p = from c in db.states
                select c;

            var ddllist = new List<OptionItem<string>>();

            foreach (var row in p)
            {
                model.testcountries = new List<OptionItem<string>>()
                {
                    new OptionItem<string>() {DisplayMember =row.Name, CountryCode = row.country, Value = row.Id.ToString()},
                };
            }

Open in new window


This works but it's hard coded:
   model.testcountries = new List<OptionItem<string>>()
        {
           new OptionItem<string>() { DisplayMember = "MO", CountryCode = "US", Value = "1" },
            new OptionItem<string>() { DisplayMember = "PO", CountryCode = "US", Value = "2" },
            new OptionItem<string>() { DisplayMember = "LO", CountryCode = "CA", Value = "3" }
        };

Open in new window

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
2 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 40460378
Well normally I would say you should just create the list before the loop and add a new item for every row but in this case you should be able to replace the whole code (lines 4-15) with:
model.testcountries = (from row in db.states select new OptionItem<string>() { DisplayMember = row.Name, CountryCode = row.country, Value = row.Id.ToString() }).ToList();

Open in new window

I can't really test this, only with some fake classes I created to match your code (I think).

So just for completeness when the above LINQ is not an option you would do something like this (which is basically just a slight reshuffle of your posted code):
            var p = from c in db.states
                    select c;

            var ddllist = new List<OptionItem<string>>();

            foreach (var row in p) {
                ddllist.Add(
                    new OptionItem<string>() {DisplayMember =row.Name, CountryCode = row.country, Value = row.Id.ToString()}
                );
            }

            model.testcountries = ddllist;

Open in new window

0
 
LVL 7

Author Comment

by:Camillia
ID: 40460523
thanks, let me try it. I spent a lot of time on this. It's ok if it's not done in LINQ. I just need to get it done as I have a demo.
I'll post back.
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

726 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