No Repeating Data

2 posts, 0 answers
  1. Wendy Hunt
    Wendy Hunt avatar
    50 posts
    Member since:
    Nov 2009

    Posted 18 Aug 2010 Link to this post

    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
  2. Cori
    Cori avatar
    562 posts
    Member since:
    Jul 2010

    Posted 18 Aug 2010 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top