If you add a guard for both being null you can simplify by coalescing your null
s.
if (model.ValidFrom == null && model.ValidTo == null)
{
throw new InvalidOperationException();
}
var fromDate = model.ValidFrom ?? DateTime.MinValue;
var toDate = model.ValidTo ?? DateTime.MaxValue;
dalList = dalList.Where(s =>
DbFunctions.TruncateTime(s.ValidFrom) >= dateFrom.Date
&& DbFunctions.TruncateTime(s.ValidTo) <= dateTo.Date).AsQueryable();
As long as you are using a provider that handles the full date range of .Net's DateTime
type ( 01/01/0001 to 31/12/9999 ).
You could also use the GetValueOrDefault(T default)
method of Nullable<T>
:
var fromDate = model.ValidFrom.GetValueOrDefault(DateTime.MinValue);
var dateTo = model.ValidTo.GetValueOrDefault(DateTime.MaxValue);
Of course, if you remove the guard this code will allow you to specify neither and return all results (after min date but before max date == all).
As always, there are lots of ways to skin a cat. Note that I wrote the above straight into code review so it might not work or compile :)
I would imagine that the solution given by Abbas the solution given by Abbas would be more performant though.
If you add a guard for both being null you can simplify by coalescing your null
s.
if (model.ValidFrom == null && model.ValidTo == null)
{
throw new InvalidOperationException();
}
var fromDate = model.ValidFrom ?? DateTime.MinValue;
var toDate = model.ValidTo ?? DateTime.MaxValue;
dalList = dalList.Where(s =>
DbFunctions.TruncateTime(s.ValidFrom) >= dateFrom.Date
&& DbFunctions.TruncateTime(s.ValidTo) <= dateTo.Date).AsQueryable();
As long as you are using a provider that handles the full date range of .Net's DateTime
type ( 01/01/0001 to 31/12/9999 ).
You could also use the GetValueOrDefault(T default)
method of Nullable<T>
:
var fromDate = model.ValidFrom.GetValueOrDefault(DateTime.MinValue);
var dateTo = model.ValidTo.GetValueOrDefault(DateTime.MaxValue);
Of course, if you remove the guard this code will allow you to specify neither and return all results (after min date but before max date == all).
As always, there are lots of ways to skin a cat. Note that I wrote the above straight into code review so it might not work or compile :)
I would imagine that the solution given by Abbas would be more performant though.
If you add a guard for both being null you can simplify by coalescing your null
s.
if (model.ValidFrom == null && model.ValidTo == null)
{
throw new InvalidOperationException();
}
var fromDate = model.ValidFrom ?? DateTime.MinValue;
var toDate = model.ValidTo ?? DateTime.MaxValue;
dalList = dalList.Where(s =>
DbFunctions.TruncateTime(s.ValidFrom) >= dateFrom.Date
&& DbFunctions.TruncateTime(s.ValidTo) <= dateTo.Date).AsQueryable();
As long as you are using a provider that handles the full date range of .Net's DateTime
type ( 01/01/0001 to 31/12/9999 ).
You could also use the GetValueOrDefault(T default)
method of Nullable<T>
:
var fromDate = model.ValidFrom.GetValueOrDefault(DateTime.MinValue);
var dateTo = model.ValidTo.GetValueOrDefault(DateTime.MaxValue);
Of course, if you remove the guard this code will allow you to specify neither and return all results (after min date but before max date == all).
As always, there are lots of ways to skin a cat. Note that I wrote the above straight into code review so it might not work or compile :)
I would imagine that the solution given by Abbas would be more performant though.
If you add a guard for both being null you can simplify by coalescing your null
s.
if (model.ValidFrom == null && model.ValidTo == null)
{
throw new InvalidOperationException();
}
var fromDate = model.ValidFrom ?? DateTime.MinValue;
var toDate = model.ValidTo ?? DateTime.MaxValue;
dalList = dalList.Where(s =>
DbFunctions.TruncateTime(s.ValidFrom) >= dateFrom.Date
&& DbFunctions.TruncateTime(s.ValidTo) <= dateTo.Date).AsQueryable();
As long as you are using a provider that handles the full date range of .Net's DateTime
type ( 01/01/0001 to 31/12/9999 ).
You could also use the GetValueOrDefault(T default)
method of Nullable<T>
:
var fromDate = model.ValidFrom.GetValueOrDefault(DateTime.MinValue);
var dateTo = model.ValidTo.GetValueOrDefault(DateTime.MaxValue);
Of course, if you remove the guard this code will allow you to specify neither and return all results (after min date but before max date == all).
As always, there are lots of ways to skin a cat. Note that I wrote the above straight into code review so it might not work or compile :)
I would imagine that the solution given by Abbas would be more performant though.
If you add a guard for both being null you can simplify by coalescing your null
s.
if (model.ValidFrom == null && model.ValidTo == null)
{
throw new InvalidOperationException();
}
var fromDate = model.ValidFrom ?? DateTime.MinValue;
var toDate = model.ValidTo ?? DateTime.MaxValue;
dalList = dalList.Where(s =>
DbFunctions.TruncateTime(s.ValidFrom) >= dateFrom.Date
&& DbFunctions.TruncateTime(s.ValidTo) <= dateTo.Date).AsQueryable();
As long as you are using a provider that handles the full date range of .Net's DateTime
type ( 01/01/0001 to 31/12/9999 ).
Of course, if you remove the guard this code will allow you to specify neither and return all results (after min date but before max date == all).
As always, there are lots of ways to skin a cat. Note that I wrote the above straight into code review so it might not work or compile :)
I would imagine that the solution given by Abbas would be more performant though.
If you add a guard for both being null you can simplify by coalescing your null
s.
if (model.ValidFrom == null && model.ValidTo == null)
{
throw new InvalidOperationException();
}
var fromDate = model.ValidFrom ?? DateTime.MinValue;
var toDate = model.ValidTo ?? DateTime.MaxValue;
dalList = dalList.Where(s =>
DbFunctions.TruncateTime(s.ValidFrom) >= dateFrom.Date
&& DbFunctions.TruncateTime(s.ValidTo) <= dateTo.Date).AsQueryable();
As long as you are using a provider that handles the full date range of .Net's DateTime
type ( 01/01/0001 to 31/12/9999 ).
You could also use the GetValueOrDefault(T default)
method of Nullable<T>
:
var fromDate = model.ValidFrom.GetValueOrDefault(DateTime.MinValue);
var dateTo = model.ValidTo.GetValueOrDefault(DateTime.MaxValue);
Of course, if you remove the guard this code will allow you to specify neither and return all results (after min date but before max date == all).
As always, there are lots of ways to skin a cat. Note that I wrote the above straight into code review so it might not work or compile :)
I would imagine that the solution given by Abbas would be more performant though.
If you add a guard for both being null you can simplify by coalescing your null
s.
if (model.ValidFrom == null && model.ValidTo == null)
{
throw new InvalidOperationException();
}
var fromDate = model.ValidFrom ?? DateTime.MinValue;
var toDate = model.ValidTo ?? DateTime.MaxValue;
dalList = dalList.Where(s =>
DbFunctions.TruncateTime(s.ValidFrom) >= dateFrom.Date
&& DbFunctions.TruncateTime(s.ValidTo) <= dateTo.Date).AsQueryable();
As long as you are using a provider that handles the full date range of .Net's DateTime
type ( 01/01/0001 to 31/12/9999 ).
Of course, if you remove the guard this code will allow you to specify neither and return all results (after min date but before max date == all).
As always, there are lots of ways to skin a cat. Note that I wrote the above straight into code review so it might not work or compile :)
I would imagine that the solution given by Abbas would be more performant though.
If you add a guard for both being null you can simplify by coalescing your null
s.
if (model.ValidFrom == null && model.ValidTo == null)
{
throw new InvalidOperationException();
}
var fromDate = model.ValidFrom ?? DateTime.MinValue;
var toDate = model.ValidTo ?? DateTime.MaxValue;
dalList = dalList.Where(s =>
DbFunctions.TruncateTime(s.ValidFrom) >= dateFrom.Date
&& DbFunctions.TruncateTime(s.ValidTo) <= dateTo.Date).AsQueryable();
As long as you are using a provider that handles the full date range of .Net's DateTime
type ( 01/01/0001 to 31/12/9999 ).
Of course, if you remove the guard this code will allow you to specify neither and return all results (after min date but before max date == all).
As always, there are lots of ways to skin a cat. Note that I wrote the above straight into code review so it might not work or compile :)
If you add a guard for both being null you can simplify by coalescing your null
s.
if (model.ValidFrom == null && model.ValidTo == null)
{
throw new InvalidOperationException();
}
var fromDate = model.ValidFrom ?? DateTime.MinValue;
var toDate = model.ValidTo ?? DateTime.MaxValue;
dalList = dalList.Where(s =>
DbFunctions.TruncateTime(s.ValidFrom) >= dateFrom.Date
&& DbFunctions.TruncateTime(s.ValidTo) <= dateTo.Date).AsQueryable();
As long as you are using a provider that handles the full date range of .Net's DateTime
type ( 01/01/0001 to 31/12/9999 ).
Of course, if you remove the guard this code will allow you to specify neither and return all results (after min date but before max date == all).
As always, there are lots of ways to skin a cat. Note that I wrote the above straight into code review so it might not work or compile :)
I would imagine that the solution given by Abbas would be more performant though.