Skip to main content
Code Review

Return to Answer

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

If you add a guard for both being null you can simplify by coalescing your nulls.

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 nulls.

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 nulls.

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.

Added alternate to null coalesce
Source Link
RobH
  • 17.1k
  • 6
  • 38
  • 73

If you add a guard for both being null you can simplify by coalescing your nulls.

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 nulls.

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 nulls.

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.

added 138 characters in body
Source Link
RobH
  • 17.1k
  • 6
  • 38
  • 73

If you add a guard for both being null you can simplify by coalescing your nulls.

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 nulls.

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 nulls.

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.

Source Link
RobH
  • 17.1k
  • 6
  • 38
  • 73
Loading
lang-cs

AltStyle によって変換されたページ (->オリジナル) /