Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Iam having a list which contains data like a startdate , end date, name, location etc..

Now i want to write a condition like i want to get data if, the datetime.Today is between event startdate and end date then it should get the data to Ongoing events list for that true condition

also if event_startdate is lessthan datetime.Today then it should get data to pastevents list.

also id event start date is greater than datetime.Today then it should get data to Upcommingevents list.

I wrote a condition for Ongoingevents like this , it is not getting the results correctly,

C#
indexlist = saveList.Where(i => Convert.ToDateTime(i.event_startdate) <= DateTime.Today).Union(indexlist.Where(i => Convert.ToDateTime(i.event_endDate) >= DateTime.Today)).ToList();


For Upcomming Events
C#
indexlist = saveList.Where(i => Convert.ToDateTime(i.event_startdate) > DateTime.Today).ToList();


For Past events

C#
indexlist = saveList.Where(i => Convert.ToDateTime(i.event_startdate) < DateTime.Today).Union(indexlist.Where(i => Convert.ToDateTime(i.event_endDate) < DateTime.Today)).ToList();


How to correct the statements and get the correct results

C#
public class Event_Detail_Content
    {
        public string event_name { get; set; }
        public string event_location { get; set; }
        public string event_startdate { get; set; }
        public string event_endDate { get; set; }

        public int count { get; set; }
    }

public sealed partial class eventspage:page
{
List<Event_Detail_Content> indexlist = new List<Event_Detail_Content>();

List<Event_Detail_Content> saveList = new List<Event_Detail_Content>();
}


IN the eventspage class all athe above statements are written.
Please help me.

What I have tried:

C#
indexlist = saveList.Where(i => Convert.ToDateTime(i.event_startdate) <= DateTime.Today).Union(indexlist.Where(i => Convert.ToDateTime(i.event_endDate) >= DateTime.Today)).ToList();


For Upcomming Events
C#
indexlist = saveList.Where(i => Convert.ToDateTime(i.event_startdate) > DateTime.Today).ToList();


For Past events

C#
indexlist = saveList.Where(i => Convert.ToDateTime(i.event_startdate) < DateTime.Today).Union(indexlist.Where(i => Convert.ToDateTime(i.event_endDate) < DateTime.Today)).ToList();
Posted
Updated 7-Jul-16 2:06am

i am not exactly sure whether this will resolve your problem,from my understanding the following solution may help you to resolve the current situation

* Only for Ongoing Events

had you try this instead of your existing Code :
C#
indexlist = saveList.Where(i = Convert.ToDateTime(i.event_startdate) <= DateTime.Today && Convert.ToDateTime(i.event_startdate) >= DateTime.Today).ToList();





C#
indexlist = saveList.Where(i => Convert.ToDateTime(i.event_startdate) <= DateTime.Today).Union(indexlist.Where(i => Convert.ToDateTime(i.event_endDate) >= DateTime.Today)).ToList();
 
Share this answer
 
v2
HI
@vijeshkumarvijayan

Thank You that worked for OnGoing Events

but for Past Events i wrote like this
C#
indexlist = saveList.Where(i => Convert.ToDateTime(i.event_startdate) < DateTime.Today).Union(indexlist.Where(i => Convert.ToDateTime(i.event_endDate) < DateTime.Today)).ToList();


It is showing the OnGoing events also(for Ex: If an event starts on 4th July 2016 and ends on 7th July 2016, it should not be shown in Past events, because the event is stillcontinuing). Can u help me in this.
 
Share this answer
 
you can reuse same code and change condition
C#
indexlist = saveList.Where(i = Convert.ToDateTime(i.event_startdate) <= DateTime.Today && Convert.ToDateTime(i.event_startdate) <= DateTime.Today).ToList();


i think it will help you exactly
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900