The T-SQL statement is below, essentially I want to return a boolean computed field xmlHasValue
SELECT TOP 10
hrd.pkID
, etc= "etc..."
, xmlHasValue = CASE WHEN hdr.someVeryLongXml IS NULL THEN 0 ELSE 1 END
FROM MyLeftTable hdr
inner JOIN MyRightTable lines ON hdr.pkID = lines.fkID
WHERE hdr.SomeField == 123
ORDER BY hdr.pkID DESC
How can I write this in EntityFramework (Full Fx, not dotnet-core), such that EF produces the Case statement as above?
My attempt:
var query = from hdr in dbCtx.MyLeftTable
join lines in dbCtx.MyRightTable on hdr.pkID equals lines.fkID
where hdr.SomeField == 123
orderby hdr.pkID descending
select new //select into anon C# obj
{
pkID = hdr.pkID,
etc = "etc...",
xmlHasValue = hdr.someVeryLongXml //<== ??? stuck here ???
};
var anonObjList = query.AsNoTracking()
.Take(10)
.ToList(); //exec qry on the SERVER, and fill the anon object.
Is it possible to translate this following Postgresql query to EFCore?
SELECT "applic"."age" FROM (
SELECT EXTRACT(YEAR FROM age(birthdate)) :: int AS "age" FROM public.applicant
) AS "applic"
WHERE "applic"."age" < 50;
I've looked into the documents but I can't find anything helpful.
A solution that works:
var applicants = from s in this.RepositoryContext.Applicants select s;
if (query.AgeStart != null && query.AgeEnd != null)
{
applicants = applicants.Where(c => (DateTime.Today.Year - c.BirthDate.Year) >= query.AgeStart && (DateTime.Today.Year - c.BirthDate.Year) < query.AgeEnd);
}
I am trying to improve the performance of stored procedure which is currently taking more than 5 hours..
I have done below things to improve performance of this procedure-
-- i have created indexes for each table.
--Instead of count(*), i am using count(1).
But i am just wondering what to use in place of while loop, if possible, Can i use cursor? will there be any impact because i have read cursors do take a lot of time in processing..
Summary of procedure:
- Calculating row count of a table.
- for every row count selecting paramters.
- These parameters are being used by another procedure for the execution.
DECLARE #lnTotalCount INT
SELECT #lnTotalCount = COUNT(*) FROM
firstTable chksts
SELECT #lnRecordCount = 1, #lnRowsProcessed =0
WHILE #lnRecordCount <= #lnTotalCount
BEGIN
-- SET ROWCOUNT 1
SELECT #lcCKPY_REF_ID = ckck.CKPY_REF_ID,
#lnCKCK_SEQ_NO = ckck.CKCK_SEQ_NO,
#lcPYPY_ID = ckck.PYPY_ID,
#lnCKCK_CK_NO = ckck.CKCK_CK_NO,
#ldtCKCK_CASHED_DT = ckck.CKCK_CASHED_DT,
#ldtCKCK_PRINTED_DT = ckck.CKCK_PRINTED_DT,
#ldtCKCK_REISS_DT = ckck.CKCK_REISS_DT,
#lcCKCK_TYPE = ckck.CKCK_TYPE,
#lcCKCK_PAYEE_NAME = ckck.CKCK_PAYEE_NAME,
#lcCKCK_CURR_STS = LTRIM(RTRIM(chksts.[TRANSACTION])),
#lnCKST_SEQ_NO = ckck.CKST_SEQ_NO,
#lcCKCK_REISS_USUS_ID = ckck.CKCK_REISS_USUS_ID,
#lnCKCK_LOCK_TOKEN = ckck.CKCK_LOCK_TOKEN,
#ldtATXR_SOURCE_ID = ckck.ATXR_SOURCE_ID,
#lnBPID_CK_NO = chksts.SERIAL_NUMBER
FROM
firstTable chksts,
secondTable ckck,
thirdTable bpid
WHERE ckck.CKPY_REF_ID = bpid.CKPY_REF_ID
AND bpid.BPID_CK_NO = chksts.SERIAL_NUMBER
AND ckck.CKCK_SEQ_NO = bpid.CKCK_SEQ_NO
AND ckck.CKCK_CURR_STS IN ('03','X3')
AND chksts.ID = #lnRecordCount
IF(##ROWCOUNT<>0) /* 1.5 If-Else condition*/
BEGIN
EXEC #lnRetCd = ABProcedure
NULL,
NULL,
#lcCKPY_REF_ID,
#lnCKCK_SEQ_NO,
#lcPYPY_ID,
#lnCKCK_CK_NO,
#ldtCKCK_CASHED_DT,
#ldtCKCK_PRINTED_DT,
#ldtCKCK_REISS_DT,
#lcCKCK_TYPE,
#lcCKCK_PAYEE_NAME,
#lcCKCK_CURR_STS,
#lnCKST_SEQ_NO,
#lcCKCK_REISS_USUS_ID,
#lnCKCK_LOCK_TOKEN,
#ldtATXR_SOURCE_ID
SELECT #lnRowsProcessed = #lnRowsProcessed + ##ROWCOUNT
SELECT #lnRecordCount = #lnRecordCount + 1
this question is in relation to my previous question: Linq2Entity Getting Values from reference Table
Now i have the problem that i want to add a new entry in Table B. I tried something like this:
ObjectQuery<A> aTable = dbConnection.A;
ObjectQuery<B> bTable = dbConnection.B;
var data = from d in aTable //Reference dataset in Table A
where d.ID == myID
select d;
B bData = new B()
{
ID = GetNewID(),
Text = text,
A = data.First()
};
dbConnection.AddToB(bData);
but this "A = d.First()" does now work... Any ideas? Thx!
select ForumCategories.ID , ForumCategories.Title , ForumCategories.DateCreated,
CO = ( select COUNT(*) from ForumSubCategories where ForumSubCategories.CategoryID_FK = ForumCategories.ID)
from ForumCategories
var q = from fc in Context.ForumCategories
select new
{
Id = fc.ID,
Title = fc.Title,
DateCreated = fc.DateCreated
CO = fc.ForumSubCategories.Count()
};
return q;
The "join" (subquery) is implicit; it's defined in the relationship between ForumCategories and ForumSubCategories in your model. Using this syntax, the call to Count() will be done on the DB server.