This is a migrated thread and some comments may be shown as answers.

No Repeating Data

1 Answer 36 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Wendy Hunt
Top achievements
Rank 2
Wendy Hunt asked on 18 Aug 2010, 04:02 PM
Hi -

I have a column in the database that has repeating values.  I'm using that column to populate a ComboBox on my page.  I don't want the repeated values to show up, but just the one time. 

Example:

1940
1940
1940
1950
1950
1960
1970
1970
1970

So, I only want this to be populated in the ComboBox:
1940
1950
1960
1970

Is there a parameter or keyword that can be used on the aspx side or the c# side that can give me the outcome I need?

Here is my c#:
private void PopulateComboBox()
{
  IQueryable allSystemPlantAccount = system_PlantAccount.GetAllBindings();
  this.systemPlantAccountRadComboBox.DataSource = allSystemPlantAccount;
  this.systemPlantAccountRadComboBox.DataValueField = "ID";
  this.systemPlantAccountRadComboBox.DataTextField = "Year";
  this.systemPlantAccountRadComboBox.DataBind();
  this.systemPlantAccountRadComboBox.Filter = RadComboBoxFilter.Contains;
  this.systemPlantAccountRadComboBox.Sort = RadComboBoxSort.Ascending;
  this.systemPlantAccountRadComboBox.SortItems();
}

Here is my aspx:
<telerik:RadComboBox ID="systemPlantAccountRadComboBox" runat="server" Width="200px"
      style="left: 20px; top: 125px; position: absolute; text-align: right;"></telerik:RadComboBox>

Any help would be great.  Thanks!

wen

1 Answer, 1 is accepted

Sort by
0
Cori
Top achievements
Rank 2
answered on 18 Aug 2010, 06:42 PM
Hello Wendy,

To accomplish what you want to do, you'll need to create a class that implements that IEqaulityComparer interface and then pass it to Distinct LINQ method. Like so:

private void PopulateComboBox() 
  IQueryable allSystemPlantAccount = system_PlantAccount.GetAllBindings; 
  this.systemPlantAccountRadComboBox.DataSource = allSystemPlantAccount.Distinct(new YearComperer()); 
  this.systemPlantAccountRadComboBox.DataValueField = "ID"
  this.systemPlantAccountRadComboBox.DataTextField = "Year"
  this.systemPlantAccountRadComboBox.DataBind(); 
  this.systemPlantAccountRadComboBox.Filter = RadComboBoxFilter.Contains; 
  this.systemPlantAccountRadComboBox.Sort = RadComboBoxSort.Ascending; 
  this.systemPlantAccountRadComboBox.SortItems(); 
}
  
public class YearComparer : IEqualityComparer<system_PlantAccount>
    {
        public bool Equals(system_PlantAccount x, system_PlantAccount y)
        {
            return x.Year == y.Year;
        }
  
        public int GetHashCode(Broker_Sched obj)
        {
            return obj.Year.GetHashCode();
        }
    }

I hope that helps.
Tags
ComboBox
Asked by
Wendy Hunt
Top achievements
Rank 2
Answers by
Cori
Top achievements
Rank 2
Share this question
or