SolvedPrivate

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

Posted on 2014-11-22
2
57 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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

729 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