I would like to ask how to write a complex DateTime query in Entity Framework as below:
I have this code in service:
Func<DateTime, DateTime> calculate24HoursLater = (date) =>
{
if (date.DayOfWeek == DayOfWeek.Friday)
return date.AddDays(3);
if (date.DayOfWeek == DayOfWeek.Saturday)
return date.AddDays(3).Date;
if (date.DayOfWeek == DayOfWeek.Sunday)
return date.AddDays(2).Date;
return date.AddDays(1);
};
var unactionedEnquiries =
dataContext.ContactedBrokerEnquiries
.Include("ContactedBrokers")
.Where(
x => x.ContactedBrokers.All(c => c.Status == (byte)LeadStatus.Rejected) ||
x.ContactedBrokers.Any(c => c.Status == (byte)LeadStatus.New && calculate24HoursLater(c.CreatedDate) < DateTime.Now)
).OrderByDescending(c => c.CreatedDate);
The result unactionedEnquiries, I expect it should be IQueryable. It means SQL server does not execute until my next statement
However, I get exception on calculate24HoursLater(c.CreatedDate) < DateTime.Now)
This statment cannot translate into SQL statement. I know the reason but I dont know how to write that rule in Entity Framework query
Important: I dont prefer to push all of data into RAM then filter with that condition. Ideally, it should be in SQL-Server
Could you please let me know how to write them in SQL-EF statement?
You may want to take a look at possibly using the SQLFunctions Methods that are available for doing date operations in LINQ queries
Sample (Untested) Try replacing your Func definition with the following:
Func<DateTime, DateTime> calculate24HoursLater = (date) =>
{
if (date.DayOfWeek == DayOfWeek.Friday)
return SqlFunctions.DateAdd("day", 3, date).Value;
if (date.DayOfWeek == DayOfWeek.Saturday)
return SqlFunctions.DateAdd("day", 3, date).Value;
if (date.DayOfWeek == DayOfWeek.Sunday)
return SqlFunctions.DateAdd("day", 2, date).Value;
return SqlFunctions.DateAdd("day", 1, date).Value;
};
As I saw here #andrew's genius answer, you can do:
var calcDate = DateTime.Now.AddHours(-24);
var unactionedEnquiries =
dataContext.ContactedBrokerEnquiries
.Include("ContactedBrokers")
.Where(
x => x.ContactedBrokers.All(c => c.Status == (byte)LeadStatus.Rejected) ||
x.ContactedBrokers.Any(c => c.Status == (byte)LeadStatus.New && c.CreatedDate < calcDate)
).OrderByDescending(c => c.CreatedDate);
Related
I have difficulties and struggle in changing SQL query to linq.
Here is one of my example code:
string sql = "WITH cte as
(SELECT TOP(1) PART_ID
FROM [History]
WHERE PART_ID = '' AND CURRENT_WEIGHT is NULL)
UPDATE cte
SET PART_ID = #PART_ID";
Here what I have done for a part:
db.Histories
.Select(u => new
{
u.PartId,
u.CurrentWeight
})
.Where(u => u.PartId == "")
.Where(u => u.CurrentWeight == null)
.Take(1);
I have no idea to change the with cte as, update statement. Hope to receive some helps. Thank you.
use this code
var history = db.Histories
.Where(u => u.PartId == "" && u.CurrentWeight == null).Take(1)
.FirstOrDefault();
history.PartId = newValue;
db.SaveChanges();
Within my app I'm trying to develop the ability to filter my returned array of offers if they fall within a set of dates set using a datepicker.
My datepicker emits the values to two properties within a range object - this is filters.range.startDate & filters.range.endDate. Each offer in my array has the properties, offer.dates.start & offer.dates.end.
I've added the below statement in my computed property which doesn't break the computed, just returns no results regardless of dates.
Does anyone have any advice?
EDIT- Added the entire computed property with the date range statement as the last condition.
computed: {
filteredOffers() {
let filtered = this.offers.filter(offer => {
return (offer.island === this.filters.islandFilter || this.filters.islandFilter === 'All') // Island
&& (offer.starrating === this.filters.starRating || this.filters.starRating === 'All') // Star Rating
&& (offer.board === this.filters.boardBasis || this.filters.boardBasis === 'All') // Board Basis
&& (offer.duration === this.filters.duration || this.filters.duration === 'All') // Duration
&& (offer.price.from < this.filters.price) // Price
&& (this.filters.travelby === 'sea' && offer.travel.air === false || this.filters.travelby === 'All') // Sea or Air
&& (this.filters.range.startDate >= offer.dates.start && offer.dates.end <= this.filters.range.endDate) // DATE RANGE!!
});
if (this.sortby === 'ascending') {
return filtered.sort((a, b) => {
return a.price.from - b.price.from;
})
} else {
return filtered.sort((a, b) => {
return b.price.from - a.price.from;
})
}
}
}
First, I would transform your date objects to timestamp in milliseconds, just avoid some format errors when you compare.
let date = new Date();
let timestamp = date.getTime();
After that, I guess your logic is not correct, because your end date on filter should be greater than your offer end date, and your start date on filter should be smaller than your offer start date.
this.filters.range.startDate <= offer.dates.start && this.filters.range.endDate >= offer.dates.end
I am bit curious if there is any way where i can have multiple conditions dynamically in the filter expression. For example
dataSource.filter(data => data.x === a && data.x === b && data.x === c)
it can have 'n' number of conditions, all will be dynamic only.
Thanks
Not like that, no. You would have to pass in a function that accepts data or data.x and check against every possible condition.
function filterData(possibleValuesOfX: number[] = []) {
return (data: Data) => {
for (let value of possibleValuesOfX) {
if (data.x === value) {
return true;
}
}
return false;
}
}
const filteredDataSource = dataSource.filter(filterData([a, b, c]));
I have a method
List<MyType> DoQuery(bool FilterWeek) {
var result = session.QueryOver<MyType>()
.Where (r => r.isValid == 1
&& r.value1 == 2
&& r.name == "XYZ"
&& [...etc, more columns are used...]
)
// how do I go on from this point?
}
if the FilterWeek parameter is true, I want to add an extra "&& r.xyz == 1" clause to the Where criteria. If FilterWeek is false, the query is done.
How do I do that?
if (FilterWeek)
result = result.Where(r => r.xyz ==1);
//...whenever you're done, execute the query using List() or SingleOrDefault()
this:
List<MyType> DoQuery(bool FilterWeek) {
var result = session.QueryOver<MyType>()
.Where (r => r.isValid == 1
&& r.value1 == 2
&& r.name == "XYZ"
&& [...etc, more columns are used...]
);
if(FilterWeek)
result.Where(x => x.Whatever == 1)
//the query won't get executed until here
result.List();
}
So far found plenty of help to get the pagination working for a get(table) command.
What I need is to pick only few of the entries from a couple of linked tables based on a sql where statement.
I guess the query command is the one to use but in this case how do I do the pagination since that command does not take extra parameters such $config['per_page']
Thanks for the help
Without any more info to go on I think that what you're looking for is something like the following.
public function pagination_example($account_id)
{
$params = $this->uri->ruri_to_assoc(3, array('page'));
$where = array(
'account_id' => $account_id,
'active' => 1
);
$limit = array(
'limit' => 10,
'offset' => (!empty($params['page'])) ? $params['page'] : 0
);
$this->load->model('pagination_model');
$data['my_data'] = $this->pagination_model->get_my_data($where, $limit);
foreach($this->uri->segment_array() as $key => $segment)
{
if($segment == 'page')
{
$segment_id = $key + 1;
}
}
if(isset($segment_id))
{
$config['uri_segment'] = $segment_id;
}
else
{
$config['uri_segment'] = 0;
}
$config['base_url'] = 'http://'.$_SERVER['HTTP_HOST'].'/controller_name/method_name/whatever_your_other_parameters_are/page/';
$config['total_rows'] = $this->pagination_model->get_num_total_rows();// Make a method that will figure out the total number
$config['per_page'] = '10';
$this->load->library('pagination');
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$this->load->view('pagination_example_view', $data);
}
// pagination_model
public function get_my_data($where = array(), $limit = array())
{
$this->db
->select('whatever')
->from('wherever')
->where($where)
->limit($limit['limit'], $limit['offset']);
$query = $this->db->get();
if($query->num_rows() > 0)
{
$data = $query->result_array();
return $data;
}
return FALSE;
}
This should at least get you on the right track
If this isn't what you're asking I'd happy to help more if you can be a little more specific. How about some of your code.
The only other options that I can think of would be to either code a count in your select statement or not limit the query and use array_slice to select a portion of the returned array.