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.
Source: stackoverflow.com
Related Query
- Array of dates, check if date exists
- Elegant solution to check if two dates are equal, in Linq (where the second date is NOT a parameter)
- Check if a value from one array exists in another array using linq
- RavenDb 4: Check if a string of an array of strings exists in different array of strings
- Asp.Net MVC LINQ query to check if agreement already exists between 2 dates
- How to check the second element of an array in linq only if it exists
- List or Array of String Contain specific word in Html Source Code
- Linq - check if the any of the values present in an array exists in the database table
- Check if two dates completely overlaps a date range
- Check whether an array is a subset of another
- how to check if object already exists in a list
- C# Linq Where Date Between 2 Dates
- Check if KeyValuePair exists with LINQ's FirstOrDefault
- Check for any element that exists in two collections
- C# Linq to XML check if element exists
- LINQ Query problem. Need to check if exists
- Find minimal and maximal date in array using LINQ?
- How to check if all values in an array are equal
- How do you check if Record exists
- LINQ to check if ID exists in List
- C# check an element exists while using LINQ to XML
- How to check if property exists in Type in an extension method?
- Check if a key exists in a NewtonSoft JObject C#
- How to check if all the elements in an array are positive integers?
- Check Contains into an int array for null property
- Filling in missing dates using a linq group by date query
- Check if date is this date or bigger
- how to check given date exist in month or not?
- Best way to check if value exists for a key in ILookup<string, string> using linq
- LINQ Source Code Available
More Query from same tag
- Compare 2 lists and provide new list with updated quanities
- linq way to insert element in order
- How to display Grid data with 'filtered' binding list
- Does JavaScript have a similar capability as LINQ's Select statement?
- Using LINQ EF query to get all dates that fall in given months
- Making random number in Linq
- How is it possible that "RemoveAll" in LINQ is much faster than iteration?
- How can I report progress from a PLINQ query?
- Linq- preferable value in select
- How|Where are closed-over variables stored?
- Entity framework to populate a ViewModel
- Linq to SQL C# Get Products in all child/sub child Categories too slow
- Finding MAX date in LINQ query join
- Is it possible to Query a Query?
- Getting triangles using LINQ returns empty triangles
- Calculate number of days left between two dates using LINQ query
- Complex SQL to LINQ conversion with subquery
- extract id attributes strings
- How to convert db results into 2D array
- Filter a list based on another list condition
- Ordering a linq list based on a property of a child class
- Can I prevent a LINQ DataContext from using a transaction when a TransactionScope is present?
- Getting Distinct count from the list
- how to convert Database Hierarchical Data to XML using ASP.net 3.5 and LINQ
- Compare two generic type in linq to sql
- Returning multiple fields with LINQ GroupBy on a datatable
- Linq to EF system not supported exception System.NotSupportedException
- Parsing a string from a textfile and using LINQ
- How to best extract attribute values and element values with LINQ and C#
- Select multiple types in LINQ Select