?
SolvedPrivate

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

Posted on 2014-11-22
2
Medium Priority
?
59 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

770 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