locked
Logic to Filter Duplicate dates RRS feed

  • Question

  • User-543160537 posted

    Hi, 

    Can someone help me with some ideas on how to filter duplicate dates and count number of days from a list  ?

    Type Start Date End Date
    A 08/15/18 08/16/18
    A 08/17/18 08/19/18
    A 08/17/18 08/18/18

    So lets say i have above list in a C# CategoryList with CategoryType, StartDate, EndDate..

    I will loop through the list and how can i filter the duplicate date on line 3.  One line 2 we already have date from 8/17 to 8/19 so when it reads 3rd row we should identify those dates are already checked and we need to remove from the list. 

    List<Category> CategoryList = new List<Category>();

    ForEach (cate in CategoryList )
    {
    if () //what condition i have to give to check the start date and end date through out the list and filter them out ?

    }
    Monday, October 29, 2018 7:58 PM

All replies

  • User475983607 posted

    As shown the requirement is impossible.  One more column is required to establish a deterministic order.  Otherwise you'll end up with hard to find logical bugs since results sets are not ordered.  Also the fact that you need order might point to a design flaw.

    Monday, October 29, 2018 8:12 PM
  • User-543160537 posted

    One more column in the table ? Can you give me some example please ?

    Monday, October 29, 2018 8:39 PM
  • User475983607 posted
    More specifically you need a sort column. I have no idea how your app works, the significance of the dates, or what makes one record more important than another. That’s logic you need to figure out.
    Monday, October 29, 2018 9:11 PM
  • User-893317190 posted

    Hi sreekanth.jonna,

    If you only want to filter the data in client , you could use linq's  distinct method.

    Below is my code , it only compares start data and end data and doesn't compare type.If  one recored's start data and end data is the same as another , it will be filtered.

    public partial class linqDistinct : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                Category cat = new Category { Type = "A", Start = new DateTime(2018, 8, 15), End = new DateTime(2018, 8, 19) };
                Category cat1 = new Category { Type = "A", Start = new DateTime(2018, 8, 19), End = new DateTime(2018, 8, 30) };
                Category cat2 = new Category { Type = "A", Start = new DateTime(2018, 12, 15), End = new DateTime(2018, 12, 30) };
                Category cat3 = new Category { Type = "A", Start = new DateTime(2018, 8, 15), End = new DateTime(2018, 8, 19) };
                List<Category> list = new List<Category>();
                list.Add(cat);
                list.Add(cat1);
                list.Add(cat2);
                list.Add(cat3);
              list=  list.Distinct(new CateogyComparer()).ToList();
    
                Response.Write(list.Count());
               
            }
        }
    
        public class Category
        {
            public string Type { get; set; }
            public DateTime Start { get; set; }
            public DateTime End { get; set; }
        }
    
        public class CateogyComparer : IEqualityComparer<Category>
        {
            public bool Equals(Category x, Category y)
            {
            
                return x.Start.ToString() == y.Start.ToString() && x.End.ToString() == y.End.ToString();
            }
    
    
            public int GetHashCode(Category obj)
            {
                return 1;
            }
        }

    The result.

    For more information about distinct, you could refer to

    https://docs.microsoft.com/zh-cn/dotnet/api/system.linq.enumerable.distinct?redirectedfrom=MSDN&view=netframework-4.7.2#System_Linq_Enumerable_Distinct__1_System_Collections_Generic_IEnumerable___0__System_Collections_Generic_IEqualityComparer___0__

    Best regards,

    Ackerly Xu

    Tuesday, October 30, 2018 6:46 AM