I've got a strange situation.
I've got a column (smallint) that has a single value of 6
I run using a linq to retrieve records that are Validity_months old.
var query = db.table_1
.AsNoTracking()
.Include(b => b.table_2)
.ThenInclude(t => t.table_3)
.Where(e => e.Date <= DateTime.UtcNow.AddMonths(e.table_2.ValidityMonths))
.Where(e => e.Date >= DateTime.UtcNow.AddMonths(e.table_2.ValidityMonths*-1)));
This throws an MySQL Error
Error Code: 1690. BIGINT UNSIGNED value is out of range in '(database.s0.validity_months * (-(1)))'
The query above translates into SQL Query
SELECT *
FROM `table_1` AS `s`
LEFT JOIN `table_2` AS `s0` ON `s`.`table_2_id` = `s0`.`id`
LEFT JOIN `table_3` AS `s1` ON `s0`.`table_3_id` = `s1`.`id`
WHERE (`s`.`date` <= DATE_ADD(UTC_TIMESTAMP(), INTERVAL CAST(`s0`.`validity_months` AS signed) month)))
AND (`s`.`date` >= DATE_ADD(UTC_TIMESTAMP(), INTERVAL CAST(`s0`.`validity_months` * -1 AS signed) month));
This ran into MySQL throws the same error. The problem is at
CAST(`s0`.`validity_months` * -1 AS signed)
How do I negate a field in LINQ. This feels like it's a EFCore bug, but I am not sure yet.
Related
I am learning MySQL through self-practice. In a project, I want to create a transfer module using MySQL (phpMyAdmin). Unfortunately, the WHERE conditions are not working well. I execute the query using the XAMPP application. A part of the query is -
SELECT
*
FROM
(SELECT
`emp_id`,
`emp_name`,
`present_posting`,
`curr_zone`,
`office_ID1` AS `new_office`,
`Zone1` AS `new_zone`,
`office_ID1` AS `C1`,
`post`,
`preference`,
`curr_zone_id`
FROM
`transfer_applications`
WHERE
`Mutual Accepted` != 'Mutual Accepted'
ORDER BY `apID` ASC) `aa`
LEFT JOIN
(SELECT
`zone_ID`, `ZONE`, `office_ID`, `office_Vacancy`
FROM
`vacancy`) `ab` ON `aa`.`C1` = `ab`.`office_ID`
LEFT JOIN
(SELECT
`zid`, `max_min_vacancy`
FROM
`capping_vacancy`) `ac` ON `aa`.`new_zone` = `ac`.`zid`
WHERE
((`curr_zone_id` != `new_zone`
AND (`max_min_vacancy` < 150
AND `max_min_vacancy` > 1)
AND `station_Vacancy` > 0
AND `post` = 4
AND `apID` = x + 1)
OR (`curr_zone_id` = `new_zone`
AND `station_Vacancy` > 0))
The problem is that it allows transfer even if there is no vacancy available that is the minimum capping in WHERE ( max_min_vacancy > 1 ) is not working. I am unable to find out the reason why it skips this condition while all other conditions in the WHERE are working fine. Kindly help me to find out the mistake. Thanks.
The WHERE clause is connected by an OR, so if the first Boolean expression is FALSE (ie. max_min_vacancy <=1), it is still TRUE as long as the second boolean expression returns TRUE.
...
WHERE
(
(
`curr_zone_id` != `new_zone` AND
(
`max_min_vacancy` < 150 AND
`max_min_vacancy` > 1 -- if max_min_vacancy <= 1 --> FALSE
) AND
`station_Vacancy` > 0 AND
`post` = 4 AND
`apID` = x + 1
)
OR ( -- But this condition is TRUE
`curr_zone_id` = `new_zone` AND
`station_Vacancy` > 0
)
)
select *
from qmsCallAudit
where CAST(FLOOR(CAST(callauditdt AS float)) AS DATETIME)
between '2015-07-16 00:00:00' and '2015-07-16 00:00:00'
above query return records in sql query
from linq its not not working, below is application code
var query = (
from qd in db.qmsCallAuditDetails
from qm in db.qmsCallAudits
where qm.callaudit_id == qd.callaudit_id &&
qm.active == true
&& qm.process_id == processid
&& fdt >= qm.callauditdt
&& tdt <= qm.callauditdt
select qd
).AsQueryable();
string semp = frm["employee_id"];
if (semp.Length > 0)
{
int empid = Convert.ToInt32(frm["employee_id"]);
query = query.Where(q => q.qmsCallAudit.employee_id==empid);
}
below sql profile code for above linq query
exec sp_executesql N'SELECT
[Extent1].[callauditdetail_id] AS [callauditdetail_id],
[Extent1].[callaudit_id] AS [callaudit_id],
[Extent1].[subparameter_id] AS [subparameter_id],
[Extent1].[subparameterdisp_id] AS [subparameterdisp_id],
[Extent1].[score] AS [score],
[Extent1].[weightage] AS [weightage],
[Extent1].[fatal] AS [fatal],
[Extent1].[add_to_weightage] AS [add_to_weightage],
[Extent1].[auditstatus] AS [auditstatus],
[Extent1].[auditscore] AS [auditscore],
[Extent1].[auditweightage] AS [auditweightage],
[Extent1].[auditremark] AS [auditremark],
[Extent1].[createdon] AS [createdon],
[Extent1].[createdby] AS [createdby],
[Extent1].[modifiedon] AS [modifiedon],
[Extent1].[modifiedby] AS [modifiedby],
[Extent1].[active] AS [active]
FROM [dbo].[qmsCallAuditDetail] AS [Extent1]
INNER JOIN [dbo].[qmsCallAudit] AS [Extent2] ON [Extent2].[callaudit_id] = [Extent1].[callaudit_id]
WHERE (1 = [Extent2].[active]) AND ([Extent2].[process_id] = #p__linq__0) AND (#p__linq__1 >= [Extent2].CAST(FLOOR(CAST(callauditdt AS float)))[callauditdt]) AND (#p__linq__2 <= [Extent2].[callauditdt])',N'#p__linq__0 int,#p__linq__1 datetime2(7),#p__linq__2 datetime2(7)',#p__linq__0=2,#p__linq__1='2015-07-16 00:00:00',#p__linq__2='2015-07-16 00:00:00'
Whats your error? Are your ftd and tdt already dateTime objects?
I'm trying to figure out how to write a linq query that will return equivalent results to the sql query below. The problem I'm having has to do with the two select count queries included in the select list of the main query. I need to get counts of two different types of records from the PaymentHistory table for the last year. Can the equivalent of this be written using linq? Preferrably using lambda syntax.
select ieinum, serviceaddrkey,
(select count(*) from PaymentHistory where serviceaddrid = serviceaddrkey
and PostDate >= DateAdd(year, -1 , GetDate())
and SetID = 100) as ReturnedFees,
(select count(*) from PaymentHistory where serviceaddrid = serviceaddrkey
and PostDate >= DateAdd(year, -1 , GetDate())
and SetID = 101) as CorrectedReturnedFees
from Serviceaddr
Any help will be great.
Perhaps something like this:
from s in Serviceaddr
let ph = PaymentHistory.Where(p => s.serviceaddrkey == p.serviceaddrkey &&
p.PostDate >= DateTime.Now.AddYears(-1))
select new
{
s.ieinum,
s.serviceaddrkey,
ReturnedFees = ph.Count(p => p.SetID == 100),
CorrectedReturnedFees = ph.Count(p => p.SetID == 101)
}
Why is this not working
...
) as Data
WHERE UNIX_TIMESTAMP(Data.DateTime) <= UNIX_TIMESTAMP(SELECT DateTime from mytable WHERE ID = $inputID)
It seems to work if I don't have this embedded sql statement, but the sql statement works on its own also so I'm not sure why the combination is causing a failure.
It's telling me
check to your MySQL server version for the right syntax to use near
'SELECT DateTime from mytable WHERE ID = 1008)' at line 1
Try surrounding the SQL with additional parens ():
WHERE UNIX_TIMESTAMP(Data.DateTime) <= UNIX_TIMESTAMP((SELECT DateTime from mytable WHERE ID = $inputID))
Use this:
...
) as Data
WHERE UNIX_TIMESTAMP(Data.DateTime) <= (SELECT UNIX_TIMESTAMP(DateTime) from mytable WHERE ID = $inputID)
If DateTime has type DateTime than you can also use:
...
) as Data
WHERE Data.DateTime <= (SELECT DateTime from mytable WHERE ID = $inputID)
This really puzzled for hours, I searched all over the internet, but got no working solution. Can someone point where the problem is ... thanks !
I created my own dialect class
public class MySQLDialectExtended : MySQLDialect
{
public MySQLDialectExtended()
{
RegisterFunction("date_add_interval", new SQLFunctionTemplate(NHibernateUtil.Date, "date_add(?1, INTERVAL ?2 ?3)"));
}
}
Then I try to use it as follows:
query.Append(
" ( date_add_interval(D.ApprovalDate, 1, YEAR) < current_timestamp() < date_add_interval(D.RenewalDate, -1, YEAR) )");
It fails with following exception:
NHibernate.Hql.Ast.ANTLR.QuerySyntaxException : Exception of type 'Antlr.Runtime.NoViableAltException' was thrown. near line 1, column 677
where the column number is at the end of the first 'YEAR' word.
Edit:
here is my configuration
<property name="dialect">MyCompanyName.MySQLDialectExtended, MyCompanyName</property>
<property name="hbm2ddl.keywords">none</property>
Can you post the whole NHibernate query?
UPDATE: Well, the query is obviously malformed and erroneous:
Select distinct D from MBIgnition.Core.Domain.Model.Deal D where 1=1 and
( (D.MortgageStatus = 30 ) or
(D.MortgageStatus = 35 ) or
(D.MortgageStatus = 40 ) or
(D.MortgageStatus = 45 ) or
(D.MortgageStatus = 55 ) or
(D.MortgageStatus = 50 ) ) and
// next line is erroneous as the first AND operator does not have a lefthand side operand
(( and ( date_add_interval(D.ApprovalDate, 1, YEAR) < current_timestamp() < date_add_interval(D.RenewalDate, -1, YEAR) ) ) )
As you can see, there's an AND operator in your code without any lefthand-side arguments. There should be something wrong with your HQL. Double check it again and if you couldn't pinpoint the error it will be useful to post the HQL or the criteria building code here.