Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
SolvedPrivate

Rewriting this code another way

Posted on 2014-11-22
14
Medium Priority
?
66 Views
Last Modified: 2016-02-16
I'm so stuck on this. I know what the error is but I don't know how to fix it so I'm trying to rewrite it another way.

(this is the error : Unrecognized expression node: ListInit )

The code below gives me a LINQ error. The solution is here but I don't know how to fix it Solution to error

1. This gives me an error:
 public List<SelectListItemWithAttributes> DropDownDatas
         {
            get
            {

                DataClasses1DataContext db = new DataClasses1DataContext();
             

                var DropDownDataItems = (
                    from c in db.states

                    select new SelectListItemWithAttributes()
                    {
                        Text = c.Name,
                        Value = c.Id.ToString(),
                        HtmlAttributes = new Dictionary<string, string> { { "data-countryid", c.country.ToString().ToLower() } }
                   }).ToList();

                DropDownDataItems.Insert(0, new SelectListItemWithAttributes() { Text = "-- Select --", Value = "", HtmlAttributes = new Dictionary<string, string> { { "data-callback", "false" } } });

             return DropDownDataItems;
             

            }
        }

Open in new window


2. So, I rewrote it this way but this isn't working because I init the list
var myList = new List<SelectListItemWithAttributes>();
and this comes out as empty

public List<SelectListItemWithAttributes> DropDownDatas
         {
            get
            {

                DataClasses1DataContext db = new DataClasses1DataContext();

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

                var myList = new List<SelectListItemWithAttributes>(); //this is wrong

                foreach (var row in s)
                {
                    foreach (var item in myList)
                    {
                        item.Text = row.Name;
                        item.Value = row.Id.ToString();
                        item.HtmlAttributes = new Dictionary<string, string>
                        {
                            {"data-countryid", row.country.ToString().ToLower()}
                        };

                     myList.Add(item);
                    }
                  
                }

               return myList;
                

            }
        }
    }

Open in new window


And this is the definition of list:

public class SelectListItemWithAttributes : SelectListItem
    {
        public IDictionary<string, string> HtmlAttributes { get; set; }

        
    }

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
  • 7
  • 7
14 Comments
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40460204
While I am trying the simplified version (without --select--, etc), take a look at this:

private void PopulateMyListBox()
{
DataClasses1DataContext db = new DataClasses1DataContext();
ListItem[] s = (from c in db.states
                           select new ListItem(c.name)).ToArray();

           cmbox1.Items.AddRange(outputs);
}

Open in new window

0
 
LVL 7

Author Comment

by:Camillia
ID: 40460206
Thank you so much for taking a look. I've been trying to get this done all afternoon. I was about to try something else.

I need this back in there. I think this is what throws the error

  item.HtmlAttributes = new Dictionary<string, string>
                        {
                            {"data-countryid", row.country.ToString().ToLower()}
                        };
0
 
LVL 7

Author Comment

by:Camillia
ID: 40460207
Yes, this line causes the error. I just took it out and it runs without but i need that back.

                     
  HtmlAttributes = new Dictionary<string, string> { { "data-countryid", c.country.ToString().ToLower() } }

Open in new window


In this code. It's a LINQ error so there has to be a different way of doing this..
var DropDownDataItems = (
                    from c in db.states

                    select new SelectListItemWithAttributes()
                    {
                        Text = c.Name,
                        Value = c.Id.ToString(),
                        HtmlAttributes = new Dictionary<string, string> { { "data-countryid", c.country.ToString().ToLower() } }
                    }).ToList();

Open in new window

0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40460211
I tried what I gave you using a database and it populates the drop box. I am kind of new to LINQ.

So, populating the control works with a data table. I am assuming your coding upto db.states is correct. If it is then it should work for you as well.

Did you try it as I had it? Please give it a try.

Mike
0
 
LVL 7

Author Comment

by:Camillia
ID: 40460213
Yes, as you have it works. But I need that line of code back. That's what breaks it. I'll continue my search for an answer.
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40460216
Is it suppose to add something to the drop box? if so, how the final data should look like?
0
 
LVL 7

Author Comment

by:Camillia
ID: 40460218
it should look like this

<option value="1" data-countryid="US">MO</option>
0
 
LVL 7

Author Comment

by:Camillia
ID: 40460220
I tried it like this but got an error that index must be within the bounds of list.

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

                var myList = new List<SelectListItemWithAttributes>();
   foreach (var row in s)
                {
                    myList.Insert(row.Id, new SelectListItemWithAttributes() { Text = row.Name, Value = row.Id.ToString(), HtmlAttributes = new Dictionary<string, string> { { "data-ccountryid", row.country } } });
                }

Open in new window

0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40460221
Can you add this dictionary data to your db.states before hand (before it gets to LINQ)? It seems you are trying to add two fields to the drop box which I think will not work.

I am testing something new, brb
0
 
LVL 7

Author Comment

by:Camillia
ID: 40460228
I have it in db.states. I need to add it to the dropdown as an extra attribute. It's ok. Someone else might be able to look at this question.
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40460232
my dictionary was called listBoxes, change it to whatever you have. But it seems your dictionary is anonymous.

foreach (var d in listBoxes)
        {
            DropDownList1.Items.Insert(0, ">" + d.Value.ToString());
        }

Open in new window


This code comes after the first one. Remove ">" from code. I wanted to have a visual confirmation items added.

FYI, I will away for a while.

Mike
0
 
LVL 34

Accepted Solution

by:
Mike Eghtebas earned 2000 total points
ID: 40460276
DataClasses1DataContext db = new DataClasses1DataContext();
ListItem[] s = (from c in db.states
                           select new ListItem(c.name)).ToArray();

           ddllist.Items.AddRange(outputs);

ddllist.DisplayMember="EnterFieldNameHere";   //like "State"
ddllist.ValueMember="EnterRecordIDFieldHere"; // like "StateID"

Open in new window

0
 
LVL 7

Author Comment

by:Camillia
ID: 40460280
that might work I'll try it and add the custom htmlattribute back and see
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40460680
DataClasses1DataContext db = new DataClasses1DataContext();
ListItem[] s = (from c in db.states
                           select new ListItem(c.StateID, c.State)).ToArray();

           ddllist.DataSource =s;

ddllist.DisplayMember="EnterFieldNameHere";   //like "State"
ddllist.ValueMember="EnterRecordIDFieldHere"; // like "StateID"

?

With ddllist.Items.Add it adds a single item, but with ddllist.Datasource =Table1, it add both items (ID and Name).
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

670 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