ASP.NET - dropdown binding from table context in linq

Asked By siv h on 22-Mar-13 10:37 AM

hello,

i could bind the data form tables to dropdownlist in asp .net using linq.

ex:

var res=from n in dc. contacts where n.location==Hyd select new{n.Id,n.Name};

var res1=from n in dc. Morecontacts where n.location==Hyd select new{n.Id,n.Name};

dropdown1.Datasouce=res;

dropdown1.DataTextField="Name";

dropdown1.DataValueField="Id";

dropdown1.DataBind();

dropdown1.Datasouce=res1;

dropdown1.DataTextField="Name";

dropdown1.DataValueField="Id";

dropdown1.DataBind();

colud u please help to me.

thanks
Danasegarane Arunachalam replied to siv h on 22-Mar-13 11:29 PM
If you rebind the dropdown then the old values will be last. You need to get the result using join and then bind it into the dropdown list

Try this one(untested).

var results=from dc in dc.contacts join dcm in dc.Morecontacts on dc.id equals dcm.id
       where dc.location="Hyd"
       select new{n.Id,n.Name};

dropdown1.Datasouce=results;

dropdown1.DataTextField="Name";

dropdown1.DataValueField="Id";

dropdown1.DataBind();