Victor
Victor

Reputation: 555

Bind the values without duplicates from database into dropdown using LINQ

I have a small issue binding the values into a dropdown using LINQ in ASP.NET code-behind.

var clientquer = from i in Entity.New_Bank select i;
//var q = (from s in names
//         select s).Distinct();
// var getlendername = (from db in mortgageentity.New_Lender group db by          db.Bank_Name into t select t.Key).ToList();
if (clientquer.Count() > 0)
{
    ddlbankname.DataSource = clientquer.ToList(); 
    ddlbankname.DataValueField = "Bank_ID2";
    ddlbankname.DataTextField = "Bank_Name";
    ddlbankname.DataBind();
}

It is binding with duplicate values, but I don't want bind duplicate values. I'm trying to solve this by using a group by clause, but it is not working.

How can this be done?

Upvotes: 1

Views: 2497

Answers (1)

p.campbell
p.campbell

Reputation: 100587

Try this:

 var clientquer = Entity.New_Bank
                        .Select(x=> new {Bank_ID2=x.Bank_ID2,
                                         Bank_Name=x.Bank_Name})
                        .Distinct();

Then bind your dropdown as normal.

Upvotes: 2

Related Questions