Results 1 to 5 of 5

Thread: From singular to collection but select single

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    From singular to collection but select single

    Hi.
    originally the "solution" had a singular class which had:

    DateFrom
    DateTo
    StartAt
    EndAt


    properties. all DateTime values

    constructor:

    Code:
    ID = Guid.Empty;
                Start = StringUtils.GetBaseTime().Add(new TimeSpan(0, 0, 1));
                Finish = StringUtils.GetBaseTime().Add(new TimeSpan(23, 59, 59));
                FinishDate = StringUtils.GetBaseTime().Add(new TimeSpan(18250, 0, 0, 0));
                StartDate = StringUtils.GetBaseTime().Add(new TimeSpan(0, 0, 1));
    Thats fine.

    There is also a method called "IsValid()" which does:

    Code:
    public bool IsValid()
            {
                DateTime now = DateTime.Now;
                if (now.TimeOfDay < Start.TimeOfDay)
                    return false;
    
                if (now.TimeOfDay > Finish.TimeOfDay)
                    return false;
    
                var logicalDay = EndOfDayHelper.GetLogicalDay(now);
    
                if (logicalDay.Start < StartDate)
                    return false;
    
                if (logicalDay.Start > FinishDate)  
                    return false;
    
                switch (logicalDay.Start.DayOfWeek)
                {
                    case DayOfWeek.Sunday:
                        return (Sunday);
                    case DayOfWeek.Monday:
                        return (Monday);
                    case DayOfWeek.Tuesday:
                        return (Tuesday);
                    case DayOfWeek.Wednesday:
                        return (Wednesday);
                    case DayOfWeek.Thursday:
                        return (Thursday);
                    case DayOfWeek.Friday:
                        return (Friday);
                    case DayOfWeek.Saturday:
                        return (Saturday);
                }
    
                return true;
            }
    thats fine.

    but now, there is a requirement to be able to hold a collection of these dates/times. Thats ok, ive created a collection to do so.

    Now we need to do the same IsValid() validation but only selecting a singular item, making sure that the item in question from the collection is suited for the present time.

    my query seems to work but i am not entirely sure how to handle it for the scenario of Dates (with times) only, so the same logic as above is applied.

    please ignore the duplicate code for now.

    Code:
    public bool IsValid()
            {
                DateTime now = DateTime.Now;
                var logicalDay = EndOfDayHelper.GetLogicalDay(now);
    
                    var validityPeriodsQueryable = Enumerable.Cast<ValidityPeriodItem>(ValidityPeriods);
    
    
                    if (validityPeriodsQueryable.Count() == 0) 
                    {
                        switch (logicalDay.Start.DayOfWeek)
                        {
                            case DayOfWeek.Sunday:
                                return (Sunday);
                            case DayOfWeek.Monday:
                                return (Monday);
                            case DayOfWeek.Tuesday:
                                return (Tuesday);
                            case DayOfWeek.Wednesday:
                                return (Wednesday);
                            case DayOfWeek.Thursday:
                                return (Thursday);
                            case DayOfWeek.Friday:
                                return (Friday);
                            case DayOfWeek.Saturday:
                                return (Saturday);
                        }
    
                        return true;
                    }
    
                    var tmpEligibleItem = (from ValidityPeriodItem currentItem in validityPeriodsQueryable
                                        where now.TimeOfDay > currentItem.Start.TimeOfDay && now.TimeOfDay < currentItem.Finish.TimeOfDay
                                        
                                        orderby currentItem.Start ascending // always the most present/current one. NOT in the future.
                                        select currentItem
                                        ); 
    
    
                    var eligibleItem = tmpEligibleItem.FirstOrDefault();
    
                    if (eligibleItem != null) // we have rules to apply
                    {
                        if (eligibleItem.AllowUsage.HasValue && eligibleItem.AllowUsage.Value == false) 
                        {
                            return false;
                        }
    
                        if (now.TimeOfDay < eligibleItem.Start.TimeOfDay)
                            return false;
    
                        if (now.TimeOfDay > eligibleItem.Finish.TimeOfDay)
                            return false;
    
                        
                        if (logicalDay.Start < eligibleItem.StartDate)
                            return false;
    
                        if (logicalDay.Start > eligibleItem.FinishDate)
                            return false;
    
                        switch (logicalDay.Start.DayOfWeek)
                        {
                            case DayOfWeek.Sunday:
                                return (Sunday);
                            case DayOfWeek.Monday:
                                return (Monday);
                            case DayOfWeek.Tuesday:
                                return (Tuesday);
                            case DayOfWeek.Wednesday:
                                return (Wednesday);
                            case DayOfWeek.Thursday:
                                return (Thursday);
                            case DayOfWeek.Friday:
                                return (Friday);
                            case DayOfWeek.Saturday:
                                return (Saturday);
                        }
    
                        return true;
                    }
           } else { /*.... old code as above.... */}
    }
    I hope it makes sense, but understand the confusion/understanding of my explanation!

    basically, the same logic should apply as "before". but because we are now dealing with a collection, I only ever want to get a singular (if applicable) entity out from the collection and perform the logic based on that entity. but the query I am using, not sure if it is the correct query. I know it works for the time of day but im sure I can improve that some how, and also fix any issues which involves dates.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: From singular to collection but select single

    I think you over thunk it... the IsValid should be a method of your singular object... the collection has no bearing on the issue. Then all you need to do is get the item from your collection, and test its IsValid method and see what it returns. The IsValid method shouldn't care if it part of a collection or not, it simply deals with the data within the instance of that singular object.

    -tg

    EDIT - and no, you should NOT have a IsValid method on the collection itself... to do so would semantically indicate that it is to check to see if the collection is valid... which means.... what? See? So leave the IsValid in the individual class where it belongs.
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: From singular to collection but select single

    agreed but its just how things are currently :-/

    lets see...

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: From singular to collection but select single

    So, are you saying that you want all the items that are valid from the collection or just one item from the collection that is valid? If you want multiple then use Where and if you want one then use FirstOrDefault, e.g.
    csharp Code:
    1. var validItems = myCollection.Where(i => IsValid(i))
    If you want one item then you'll presumably want to slip an OrderBy in there too.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: From singular to collection but select single

    thanks.
    its not quite the syntax/code im looking for but rather the logic.

    as it happens, they dont want it the way i had thought. typical... and really annoying me as there is absolutely no spec but some how have to be psychic on the requirements.

    such a complicated system.... urgh.

    originally they had it where it was doing what its currently doing - but for a singular "period" - a period is just the StartDate/FinishDate/StartAt/EndAt properties and does some validation as shown earlier.

    now they want to cater for multiple periods. one would think that its simple - where we only select the most relevant period, from the collection, which is where a period is suitable for the current time, then perform the same validation as before.

    obviously not....

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width