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.
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 ?
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.
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.
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
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
17 Points
151 Posts
Logic to Filter Duplicate dates
Oct 29, 2018 07:58 PM|sreekanth.jonna|LINK
Hi,
Can someone help me with some ideas on how to filter duplicate dates and count number of days from a list ?
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.
All-Star
52261 Points
23317 Posts
Re: Logic to Filter Duplicate dates
Oct 29, 2018 08:12 PM|mgebhard|LINK
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.
Member
17 Points
151 Posts
Re: Logic to Filter Duplicate dates
Oct 29, 2018 08:39 PM|sreekanth.jonna|LINK
One more column in the table ? Can you give me some example please ?
All-Star
52261 Points
23317 Posts
Re: Logic to Filter Duplicate dates
Oct 29, 2018 09:11 PM|mgebhard|LINK
Contributor
3500 Points
1300 Posts
Re: Logic to Filter Duplicate dates
Oct 30, 2018 06:46 AM|Ackerly Xu|LINK
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.
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
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.