Binding Linq Query to Drop Down List

Hi,

Hi,

New to EF.  I'm trying to bind the results of a linq query to a drop down list.  Here's the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ABC
{
    
    public partial class createDoc : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HCLEntities Context = new HCLEntities();

            var query = from p in Context.Equipments
                                             orderby p.Unit
                                             select p.Unit;

            projectDropDownList.DataSource = query;
            projectDropDownList.DataBind();

        }
    }
}

Open in new window


When the code is executed, I get the following message:
Data binding directly to a store query is not supported. Instead populate a DBSet with data, for example by calling Load on the DBSet, and then bind to local data.

Not sure how to fix the error.  Suggestions would be greatly appreciated.
WhyDidntItWorkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi WhyDidntItWork;

Try the query as shown below.

var query = (from p in Context.Equipments
             orderby p.Unit
             select p.Unit).ToList();

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WhyDidntItWorkAuthor Commented:
That worked like magic!  Thank you.
0
Fernando SotoRetiredCommented:
Not a problem; glad I was able to help.  ;=)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.