1

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?

p.campbell
  • 98,673
  • 67
  • 256
  • 322
Victor
  • 555
  • 6
  • 15
  • 39

1 Answers1

2

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.

p.campbell
  • 98,673
  • 67
  • 256
  • 322
  • Hi i'm trying this code, but This one also getting the duplicated bank names. – Victor Apr 02 '11 at 05:22
  • Hi I am trying above code to get unique bank names, but it allows the duplicated bank names. – Victor Apr 02 '11 at 05:26
  • It sounds like those bank names aren't exactly duplicate. Spaces, case-sensitive, etc? Can you post a screenshot of the HTML? The IDs and names are distinct. – p.campbell Apr 02 '11 at 05:27
  • Hi i can't post screenshot because of reputation problem.But i have checking data but there is no spaces ,case-sensitive etc. – Victor Apr 02 '11 at 05:41
  • Hi Campbell, Thank you for ur giving response. It is working fine i made a small mistake in my databse that is bank names are same but ids are different that y it showing like that . – Victor Apr 02 '11 at 06:51