I've got a database tabel 'DateExcluded' containing 3 int columns : Year, Month and Day, the latter being the daynumber of the month. I want their combination evaluated in an entity query, to retrieve all rows before one year from current date like so :
var l =
(from p in c.DateExcluded
where
DbFunctions.CreateDateTime(p.Year, p.Month, p.Day, null, null, null)
<= DateTime.Now.AddYears(1)
select p).ToList();
This query always returns 0 columns which it shouldn't. Wrong use of DbFunctions.CreateDateTime?
I've got a database tabel 'DateExcluded' containing 3 int columns : Year, Month and Day.
Don't ever create a column for each year, month and day
You're creating a non-sargable query, also know as the worst performing query you can create.
The correct way is to actually use a DateTime field. Then your query is just correct without any incorrect math possible.
var l =
(from p in c.DateExcluded
where
c.DateExcluded < DateTime.Now.AddYears(1).AddDay(1)
select p)
.ToList();
If you still want to use DbFunctions.CreateDateTime, avoid null values as it won't work correctly (see why in comment by Ole EH Dufour).
You should pass 0 instead of null, like this:
DbFunctions.CreateDateTime(p.Year, p.Month, p.Day, 0, 0, 0)
Related
I have a console app that processes orders generated since 18:30 on the previous day. The method returns an array of order numbers.
The first operation is to retrieve a list of orders from a table called Docstore (which contains orders that have been printed, which are the only orders that are needed) into an array:
string[] orders;
using (var db = new TCRContext())
{
var query = from docs in db.Docstore
where docs.DateCreated >= fromDate && docs.DocumentType == "InvoiceCredit"
select docs.PrimaryKey.Trim();
orders = query.Distinct().ToArray();
}
Typically there may be at most 40 such orders. There are a couple of further considerations. First, there's a database VIEW called MatchingCustomerOrders which JOINs Orders (from the actual Order table, not the Docstore table) onto another table CPCustomers which contains a subset of all the company's Customers who participate in this particular operation (about 90 out of 2,370).
CREATE OR ALTER VIEW [dbo].[MatchingCustomerOrders]
AS
SELECT
Orders.order_no,
Orders.invoice_date
FROM
Orders INNER JOIN
dbo.CPCustomer ON Orders.customer = dbo.CPCustomer.Customer
WHERE
(dbo.CPCustomer.IsDeleted = 0)
GO
So we only need orders that match these customers. And we want to restrict them to those which match the invoice date parameters (i.e. from 18:30 the previous day to now) and also we don't want to process orders that have already been processed (hence the ProcessedInvoices part)
var query = from matches in db.MatchingCustomerOrders
where orders.Contains(matches.order_no.Trim()) &&
!(from processed in db.ProcessedInvoices select processed.order_no.Trim()).Contains(matches.order_no.Trim()) &&
matches.invoice_date >= fromDate.Date && matches.invoice_date <= toDate
select matches.order_no;
orders = query.Distinct().ToArray();
This is not particularly performant because the VIEW MatchingCustomerOrders has 90,000+ rows, and my suspicion is that LINQ is operating over the whole set. I've been banging my head trying to refactor this but none of my solutions seems to work.
I have table that the value of dateime is like this
2021-12-08 10:10:54.657
2021-12-08 10:10:41.567
2021-12-08 10:09:51.960
2021-12-08 10:10:54.657
2021-12-08 10:10:41.567
2021-12-08 10:09:51.960
2021-12-08 10:10:54.657
and I want to get that day or today (now is 8 dec 2021) . So i have tried using EF in controller :
ViewBag.CByUserToday = _db.ArrayDatas.Where(a => a.CreatedBy == user && a.CreatedDate == DateTime.Today.Date).Count();
But i still did not get the rows. When i tried to debug DateTime.Today.Date , it's said DateTime.Today = {09/12/2021 00:00:00} . But when i tried to update that createddate to '2021-12-09 00:00:00.000' it can be retrieved.
So, how to retrieve that rows that i have created today(ignoring the time) ?
Check that the column is greater than or equal to today, and less than tomorrow:
DateTime today = DateTime.Today;
DateTime tomorrow = today.AddDays(1);
ViewBag.CByUserToday = _db.ArrayDatas
.Where(a => a.CreatedBy == user
&& a.CreatedDate >= today
&& a.CreatedDate < tomorrow)
.Count();
That way, your DBMS will be able to use a suitable index on the CreatedBy and CreatedDate columns to satisfy your query.
It's usually preferable to avoid calling functions on a column when you're trying to filter it, since this means the query is not SARGable. However, according to this thread on DBA, casting a datetime/datetime2 column to date is SARGable, at least in Microsoft SQL Server.
I want to insert the date and month (which is in two datetimepicker) along with insert value select query.
I have five columns in my invoice table
Student_id, Amount, Fee_description, issue_date, month
I can insert the values for the first three columns but the remaining two are null for which I don't know where to put the datetimepicker value??
I take a datatimepicker for date and month in the design view of the form
insert into invoice (Student_id, Amount, Fee_description, issue_date, month)
select
student.Student_id,
Fee.Fee_Amount, Fee.fee_type
from
student
join
parent on student.father_nic = parent.father_nic
join
allocate_class on student.Student_id = allocate_class.Student_id
join
class on class.class_id = allocate_class.class_id
join
session on allocate_class.session_id = session.session_id
join
Fee on class.class_id = Fee.class_id
where
session.session_id = 1
and Fee.fee_type = 'Tution Fee'
and student.status = 'active'
Where to add that two that datetimpicker value in the above query?
Sure. It would look something like this:
var cmd = new SqlCommand("insert into invoice (
Student_id,
Amount,
Fee_description,
issue_date,
month
)
select student.Student_id
, Fee.Fee_Amount
, Fee.fee_type
, #issDat
, #mon
from ...", "conn str here");
cmd.Parameters.AddWithValue("#issDat", issueDateDateTimePicker.Value);
cmd.Parameters.AddWithValue("#mon", monthDateTimePicker.Value);
I've used AddWithValue to quickly explain the concept- google for a blog called "can we stop using AddWithValue already" for a long discussion on how to move away from it(it's reasonable in this context as it's not being used for filtering) but for now the concept I'm trying to relate is:
An sql statement can take a fixed value from you:
SELECT 'hello' FROM table
It can also take a fixed parameter you supply:
SELECT #pHello FROM table
Hence adding parameters and filing them with fixed values from your day time pickers (or datetime.Now or whatever) is fine, and what you should be doing to insert fixed values using an INSERT ... SELECT style statement
Side note, it isn't clear if month and issue date are related but if they're the same date then you don't need to store it twice- you can extract the month from a date at any point.
I have to make in c# a query with linq to sql. I can handle it in sql but in linq to
sql is the result not what I wanted to get.
So there is a table with:
a day, in datetime with date and time
and a kind of id
I have to count the ids for each date, the time isn't important. So the result
should be something like:
day: 2013-11-12 amountIDs: 4
People said to me, I can make a select new query and in this query I can set the day
and could count the ids, or I make a group by day. I read similar question, but it doesn't work in my case.
Could somebody help me?
I tried it with the statement below, but the days have to be grouped, so now the output is foreach datetime, like this
day: 12.12.2013 12:00:00 amountIDs: 1
day: 12.12.2013 12:10:10 amountIDs: 1
In sql I made this statement:
SELECT CONVERT(VARCHAR(20), data.dayandtime, 106) AS day, count(data.amountIds) as ids
FROM data
WHERE ( data.dayandtime >= DATEADD(day, -28, getdate()) AND (data.type = 100) AND (data.isSomething = 0) )
GROUP BY CONVERT(VARCHAR(20), data.dayandtime, 106), data.isSomthing and it works.
I saw similar cases, where people made a : from-select-new xyz statement, than I made a view of it and tried to group just the view. Like this
var query = data.GroupBy(g => g.day.Value).ToList();
var qry = from data in dbContext
group data by data.day into dataGrpd
select new
{
day= dataGrpd.Key,
amountIDs= dataGrpd.Select(x => x.Id).Distinct().Count()
};
Check This
Given the following database structure and using the EntityFramework.
Every five minutes, the 'phasecount' table gets records for each record in 'Phase'.
using (Entities db = new Entities())
{
db.ContextOptions.LazyLoadingEnabled = false;
int numberofcontrollers = (from a in db.Junctions select a).Count();
List<int> controllerids = (from b in db.Junctions select b.Id).ToList();
var configuration = (from c in db.Configurations select c).First();
DateTime laststamp = (from s in db.Stamps select s.Time).Max();
DateTime firststamp = laststamp.AddMinutes(-1 * (CountIntervalsBefore - 1) * TimeSliceLength);
var stamps = from s in db.Stamps.Include("PhaseCounts.Phase") where s.Time >= firststamp && s.Time <= laststamp orderby s.Id select s;
// check consistency; number of stamps should equal timeslices*controllers
if (stamps.Count() != CountIntervalsBefore * numberofcontrollers)
{
//counts are not available for all timeslices and controllers
//do extended consistency check (and use dummy data?)
}
}
I want to select for one hour each phasecount for all phases.
stamps normally equals 72, ie 12 5-minute slices * 6 junctions.
If it doesn't equal 72, how to determine which phases and which timestamps have missing data?
My first thoughts on a solution; this may not be the optimal search method but it should work.
Take the earliest timestamp in the group, and check that you do indeed have the correct number of records with that timestamp (from what you've said, I believe this is 6). From that you can tell if there are any missing from this set. Then look for the closest timestamp to the current one. If it's significantly more than about five minutes, you have a whole set missing. If it's around five minutes, check that you have the correct number of records with that timestamp, like with the first set. Repeat until either (total missing records + total found records = 72) or you run out of records. If you get to the end of the records and you still have some missing, then the earliest timestamp was not the first one, and you have a complete set missing there as well. At this point, either (total missing records + total found records = 72) or something has gone very wrong.