SolvedPrivate

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

Posted on 2014-11-22
2
52 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
2 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
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.

772 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

12 Experts available now in Live!

Get 1:1 Help Now