score:2

to check that the targetdt(datetime.now) is between startdate & enddate:

if (member.memberships.any(x=>targetdt.ticks > x.startdate.ticks && targetdt.ticks < x.enddate.ticks && x.status=="active"))
{
    // has membership on targetdt.
}  

score:2

i assume that null value for start or end date means 'open' interval

var today = datetime.now;
var activemembers = allmembers.where(m => 
    m.memberships.any(ms => 
        ms.status == "active" &&
        (ms.startdate == null || ms.startdate <= today) && (ms.enddate == null || today <= ms.enddate)));

score:2

so i would structure your main classes slightly more different from what you have now. this it to get of having to compare on strings for the status, but since i don't know exactly how the active property is set but i would assume that for a membership to be active the start and (optional) end date have to be smaller than and greater than today's date.

class member
{
    private readonly ilist<membership> memberships;

    public member()
    {
        memberships = new list<membership>();
    }

    public readonlycollection<membership> memberships 
    { 
        get
        {
            return new readonlycollection<membership>(memberships);
        }
    }
}

class membership
{
    private readonly datetime enddate;

    private readonly datetime startdate;

    public membership(datetime startdate)
    {
        this.startdate = startdate;
    }

    datetime startdate 
    {
        get
        {
            return this.startdate;
        }
    }

    datetime? enddate 
    { 
        get
        {
            return enddate;
        }
    }

    public status status
    {
        get
        {
            return calculatememebershipstatus();
        }
    }

    private status calculatememebershipstatus()
    {
        var todaysdate = datetime.today;

        if (startdate > todaysdate)
        {
            return status.deactivated;
        }

        if (!enddate.hasvalue)
        {
            return status.active;
        }

        if (todaysdate < enddate.value)
        {
            return status.active;
        }

        return status.deactivated;
    }
}

enum status
{
    active,
    deactivated
}

and when you need to find out all the members that have an active membership today you can do:

var membercollection = new list<member>();
...
var activemembers = membercollection.where(x => x.memberships.any(y=> y.status == status.active));

so this will go through your member collection and select the ones that have an active status today. although as previously said this depends on how you build the active property but this is more contained in that testing for the activeness of the membership based on the date is done within the membership object. so the consumer of this data doesn't need to know how to say that a membership is active they simply only need to know that some will be active and others will be deactivated.


Related Query

More Query from same tag