Iterating each date between Start Date and End Date using Linq

Answered Iterating each date between Start Date and End Date using Linq

  • Saturday, September 01, 2012 12:26 AM
     
     

    Hi,

    I have two date fields Start Date and End Date and allocation columns.

    How to iterates(loop) each date between Start Date and End Date to check  allocation is should not more than 100 using Linq.

    For example:

    Start Date      End Date                   Allocation      App

    7/2/2012        2/28/2013                  40%              app1

    7/2/2012        2/28/2013                  60%              app2

    7/2/2011       2/28/2013                  60%                 app3. -

    Basically I need to check each date that allocation is not 100% between the Start Date and End Date . If is less than  then user can allocate


    Basawaraj


    • Edited by Basawaraj Saturday, September 01, 2012 12:48 AM
    •  

All Replies

  • Saturday, September 01, 2012 1:59 AM
     
     

    Hi Basawaraj;

    Not sure what you need. Can you please explain in different way and show what the results would be with the sample given.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

  • Monday, September 03, 2012 8:28 AM
    Moderator
     
     

    Hi Basawaraj,

    Welcome to the MSDN forum.

    Do you mean you want to check the sum of allocations of all apps is less than 100% during the start date and end date?

    Good day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

  • Monday, September 03, 2012 11:15 AM
     
     
    Yes , i want to loop through the each date between start date and end date , and check the sum is less than 100%. Here the start date can be less than any previously entered start date.

    Basawaraj

  • Monday, September 03, 2012 12:56 PM
     
      Has Code

    Try like this. Please check the following example

    DataTable dtDateTesting = new DataTable("TestTable");
    dtDateTesting.Columns.Add("StartDate", typeof(DateTime));
    dtDateTesting.Columns.Add("EndDate", typeof(DateTime));
    dtDateTesting.Columns.Add("Allocation", typeof(string));
    dtDateTesting.Columns.Add("App", typeof(string));
    dtDateTesting.Rows.Add("7/2/2012", "2/28/2013","40%","app1");
    dtDateTesting.Rows.Add("7/2/2012", "2/28/2013","60%","app2");
    dtDateTesting.Rows.Add("7/2/2011", " 2/28/2013","60%","app3");
    var rsltData = from dtTest in dtDateTesting.AsEnumerable()
    where dtTest.Field<string>("Allocation") != "100%"
    select dtTest;
                foreach (var item in rsltData)
                {
                    
                }


    With Thanks and Regards
    Sambath Raj.C
    click "Proposed As Answer by" if this post solves your problem or "Vote As Helpful" if a post has been useful to you
    Happy Programming!

  • Tuesday, September 04, 2012 5:06 AM
     
     

    Hi,

    I need to loop through the each date and check the total sum  hen we add all the values.  There is no range for start date  (like  for example start date between 7/2/2012 and 9/30/2012) i have only start date  with only one value. like this have same for end date.

    Where condition is not like start date between 7/2/2012 and 9/30/2012 and end date between 7/2/2012 and 9/30/2012.

    It will be like this start date =7/2/2012 and end date = 12/30/2012(no individual date range for start end and end date). I need find all the dates between start date and end date and check total value of allocation column.



    Basawaraj

  • Wednesday, September 05, 2012 9:05 AM
    Moderator
     
     Answered Has Code

    Hi Basawaraj,

    In my opinion, it is more easy to do it without using LINQ. You can check this:

                    List<String> list = new List<string>();
                    foreach (var t in dataContext.Tests)
                    {
                        String startDate = t.StartDate;
                        String endDate = t.EndDate;
                        Single sum = 0;
                        if (!list.Contains(startDate + endDate))
                        {
                            list.Add(startDate + endDate);
                            foreach (var tst in dataContext.Tests)
                            {
                                if (tst.StartDate == startDate && tst.EndDate == endDate)
                                {
                                    sum = sum + tst.Allocation;
                                }
                            }
                            if (sum < 1F)
                            {
                                Console.WriteLine("Start Date: {0}, End Date: {1}", startDate, endDate);
                            }
                        }
                    }
    

    I hope this helps.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us