SolvedPrivate

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Resolve Dependency Issues 4 47
Entity Framework 7 32
Help with AsEnumerable(), LINQ 4 22
Best resource to learn entity framework 2 0
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 …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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

16 Experts available now in Live!

Get 1:1 Help Now