Codes inside Weekly returns a value. But in Monthly it returns null.
I tried to run the query in my SQL Server Management Studio and it returns the correct result. But in my code, it returns null.
I tried to breakpoint and the item.JobId is = 2.
Here is my codes.
if (item.Frequency == "Weekly")
{
if (item.StartDate.DayOfWeek.ToString() == DateTime.Now.DayOfWeek.ToString())
{
var jobname = _context.Jobs.Where(x => x.Id == item.JobId).FirstOrDefault();
if (ModelState.IsValid)
{
jobs.Id = 0;
jobs.Job = jobname.Job;
jobs.ClientName = jobname.ClientName;
jobs.ClientId = jobname.ClientId;
jobs.JobRate = jobname.JobRate;
jobs.Status = "Active";
}
}
}
else if (item.Frequency == "Monthly")
{
if (item.StartDate.Day.ToString() == DateTime.Now.Day.ToString())
{
var jobname = _context.Jobs.Where(x => x.Id == item.JobId).FirstOrDefault();
var a = jobname.Job;
if (ModelState.IsValid)
{
jobs.Id = 0;
jobs.Job = jobname.Job;
jobs.ClientName = jobname.ClientName;
jobs.ClientId = jobname.ClientId;
jobs.JobRate = jobname.JobRate;
jobs.Status = "Active";
}
}
}
And here is my SQL Query
SELECT TOP (200) Id, Job, ClientName, ClientId, JobRate, Status
FROM Jobs
WHERE (Id = 2)
It should return some value in it.
Both inner expressions of the ifs(ones controlling item.StartDate) are identical, so your problem lies in these controls. Can you make sure your code evaluates true for below condition?
if (item.StartDate.Day.ToString() == DateTime.Now.Day.ToString())
I have this SQL query that I want to translate into Linq-to-SQL:
Now here's the beginning of the Linq-to-SQL code but I'm stuck on how to group fields and get SUM :
private void GetDatas()
{
DateTime d = DateTime.Now;
using (DataClasses1DataContext dc = new DataClasses1DataContext())
{
var query = from ent in dc.STK_ABC_ENT
join det in dc.STK_ABC_DET on ent.ENT_ID equals det.ENT_ID
join art in dc.FICHES_ARTICLES on ent.ART_CODE equals art.ART_CODE
where !ent.ENT_NUM_PAL.Contains("FDR_")
&& ent.ENT_OUTDATE == null
&& ent.ENT_PICKING == null
&& ent.ENT_DATE_ENT != d
// How to group here ?
// How to get SUM ??
}
}
You can use group x by ColumnName into z to group a column.
When you want to group multiple columns you can use group x by new { x.Column1, x.Column2 } into z.
When you want to group multiple columns in multiple tables you can use group new { x, y } by new { x.Column, y.Column } into z.
With Sum, just call it in select with lamda expression.
Example:
var query = from ent in dc.STK_ABC_ENT
join det in dc.STK_ABC_DET on ent.ENT_ID equals det.ENT_ID
join art in dc.FICHES_ARTICLES on ent.ART_CODE equals art.ART_CODE
where !ent.ENT_NUM_PAL.Contains("FDR_") && ent.ENT_OUTDATE == null
&& ent.ENT_PICKING == null && ent.ENT_DATE_ENT != d
group new { art, ent } by new {
art.ART_CODE,
...,
ent.ENT_DATE_ENT,
...
} into grouped
select new {
ArtCode = grouped.Key.ART_CODE,
SumPdsNet = grouped.Sum(x => x.DET_PNET),
...
}
I hope it can work for you.
This is my sql query
SELECT TOP(10)projects.stat,
wo.stat,
sevt.restype,
sevt.resid,
restype.user2,
projects.prj_id,
projects.user3,
projects.user9,
wo.wonum,
wo.jobdesc,
sevt.sesid,
sevt.restype,
sevt.type,
sevt.t_start,
sevt.t_end,
sevt. mealstart,
sevt.mealend,
sevt. melstart2,
sevt.melend2,
sevt.melstart3,
sevt.melend3,
sevt.user2,
sevt.subactid,
sevt.ot_exempt,
sevt_ex.user5,
rescat.user1
FROM schedwin.projects
INNER JOIN schedwin.wo
ON projects.prj_id = wo.prj_id
INNER JOIN schedwin.sevt
ON wo.seqnum = sevt.seqnum
INNER JOIN schedwin.rsrce
ON sevt.resid = rsrce.resid
LEFT OUTER JOIN schedwin.pers
ON rsrce.recid = pers.recid
INNER JOIN schedwin.restype
ON sevt.rtype = restype.code
INNER JOIN schedwin.rescat
ON sevt.rcat = rescat.code
LEFT OUTER JOIN schedwin.sevt_ex
ON sevt.sesid = sevt_ex.sesid
WHERE ( Ltrim(Rtrim(projects.stat)) IN ( '1', '2' ) )
AND ( Ltrim(Rtrim(wo.stat)) = '6' )
AND ( ( ( sevt.restype = 5
OR sevt.restype = 0 )
AND ( Substring(restype.user2, 2, 1) = 'F'
AND Substring(restype.user2, 6, 1) = 'S' ) )
OR ( sevt.restype = 4 ) )
AND ( sevt.type = 0 )
AND rescat.groupid = 0
AND restype.groupid = 0
AND Len(Ltrim(wo.invoice)) > 0
AND Ltrim(wo.invoice) <> 'PENDING'
AND wo.userflag1 <> 1
AND Ltrim(sevt.t_start) = '1351728000'
ORDER BY projects.prj_id,
wo.wonum
I was converting in to Linq to Entity like below
var query =
(from PROJECTS in db.PROJECTS
join WOes in db.WOes on PROJECTS.PRJ_ID equals WOes.PRJ_ID
join SEVTs in db.SEVTs on WOes.SEQNUM equals SEVTs.SEQNUM
join RSRCEs in db.RSRCEs on SEVTs.RESID equals RSRCEs.RESID
join PERS in db.PERS on RSRCEs.RECID equals PERS.RECID into PERS_join
from PERS in PERS_join.DefaultIfEmpty()
join RESTYPEs in db.RESTYPEs on new { RTYPE = SEVTs.RTYPE } equals new { RTYPE = RESTYPEs.CODE }
join RESCATs in db.RESCATs on new { RCAT = SEVTs.RCAT } equals new { RCAT = RESCATs.CODE }
join SEVT_EX in db.SEVT_EX on SEVTs.SESID equals SEVT_EX.SESID into SEVT_EX_join
//join SEVT_EX in db.SEVT_EX on new { SESID = (String)SEVTs.SESID } equals new { SESID = SEVT_EX.SESID } into SEVT_EX_join
from SEVT_EX in SEVT_EX_join.DefaultIfEmpty()
where
(new string[] { "1", "2" }).Contains((PROJECTS.STAT.TrimEnd()).TrimStart()) &&
((WOes.STAT.TrimEnd()).TrimStart() == "6") &&
(((SEVTs.RESTYPE == 5 || SEVTs.RESTYPE == 0) &&
(RESTYPEs.USER2.Substring(2 - 1, 1) == "F" && RESTYPEs.USER2.Substring(6 - 1, 1) == "S")) || (SEVTs.RESTYPE == 0)) &&
(SEVTs.TYPE == 0) &&
(RESCATs.GROUPID == 0) &&
(RESTYPEs.GROUPID == 0 )&&
(int?)(WOes.INVOICE.TrimStart()).Length > 0 &&
WOes.INVOICE.TrimStart() != "PENDING" &&
WOes.USERFLAG1 != 1 &&
(SEVTs.T_START.TrimStart()) == (Booktime)
//(SEVTs.T_START.TrimStart()) == (Booktime)
// String.Compare(SEVTs.T_START.ToString().TrimStart(), "1351728000") >= 0
//Convert.ToInt32(SEVTs.T_START.TrimStart()) >= Convert.ToInt32(Booktime)
orderby
PROJECTS.PRJ_ID,
WOes.WONUM
select new
{
PROJECTS.PRJ_ID,
PROJECTS.USER3,
PROJECTS.USER9,
WOes.WONUM,
WOes.JOBDESC,
SEVTs.SESID,
SEVTs.RESTYPE,
SEVTs.TYPE,
SEVTs.T_START,
SEVTs.T_END,
SEVTs.MEALEND,
SEVTs.MELSTART3,
SEVTs.MELSTART2,
SEVTs.MELEND2,
Column1 = SEVTs.MELSTART2,
SEVTs.MELEND3,
SEVTs.USER2,
SEVTs.SUBACTID,
SEVTs.OT_EXEMPT,
USER5 = SEVT_EX.USER5,
SEVTs.GMT_OFFSET,
SEVTs.MEALSTART,
SEVTs.STANDARD,
RESCATs.USER1,
SEVTs.RESID
}).Take(10);
Definitely my sql query is return a correct records back but my Linq is not, any have any clue what am missing here please ?
Thanks
You have this line:
(((SEVT.restype = 5 or SEVT.restype = 0) and (substring(restype.User2,2,1) = 'F' and substring(restype.User2,6,1) = 'S')) or (SEVT.RESTYPE = 4))and
and in linq you have this
((SEVTs.RESTYPE == 5 || SEVTs.RESTYPE == 0) && (RESTYPEs.USER2.Substring(2 - 1, 1) == "F" && RESTYPEs.USER2.Substring(6 - 1, 1) == "S") && SEVTs.TYPE == 0)
First, Substring in 2-1 = 1 not 2. and the Logical Operations not equal
I am using a Data Entity from the db, and a Domain Service.
I am using .net's generated code for the simple queries, like this
public IQueryable<employee> GetEmployeesByLocale(int localeID)
{
return ObjectContext.employees.Where(e => e.Locale_ID == localeID);
}
Now, I need to change the .Where section accordingly, so:
if (localeID > 0)
{
['Where' should be like .Where(e => e.Locale_ID == localeID)];
}
if (projectID > 0)
{
[IF localeID == 0, 'Where' should be like .Where(e => e.Project_ID == projectID)
Else if localeID > 0, Where should use both, sort of .Where(e => e.Locale_ID == localeID && e.Project_ID == projectID)];
}
and so on with other variables.
There are many possible combinations , which is why I was trying to use the overload for .Where(string, parameter[])
string q = string.Empty;
if (localeID > 0)
{
q = "Locale_ID = " + localeID.ToString();
}
if (projectID > 0)
{
q = q == string.Empty ? "Project_ID = " + projectID.ToString() : q + " and " + "Project_ID = " + projectID.ToString();
}
... (for other variables and fields)
...
System.Data.Objects.ObjectParameter[] param = new System.Data.Objects.ObjectParameter[1];
param[0] = new System.Data.Objects.ObjectParameter("param", 1);
return ObjectContext.employees.Where(q, param);
However, this only gives an error, because then all the fieldnames are supposedly out-of-scope/non-existing. Even if use employees.[field_name] in the string, they "don't exist"
Does anyone know of a way to use conditionals inside the .Where part of the query? Or how to create some var or object containing my query so I can just parse it?
You can use
IQueryable<Employee> emp = ObjectContext.employees;
if (localeID > 0)
emp=emp.Where(e => e.Locale_ID == localeID).AsQueryable();
if (projectID > 0)
emp=emp.Where(e => e.Project_ID == projectID).AsQueryable();
This will concatenate the where clauses
i How to use queryover (Join) for same table...example
if (!string.IsNullOrEmpty(ufResidencia) ||
!string.IsNullOrEmpty(cidadeResidencia))
{
EnderecoProspect endPros = null;
TipoEndereco tipoEnd = null;
query
.JoinQueryOver<EnderecoProspect>(x => x.Enderecos,()=> endPros)
.And(()=> endPros.Uf ==ufResidencia)
.JoinQueryOver<TipoEndereco>(x => x.TipoEndereco,()=> tipoEnd)
.And(()=> tipoEnd.Descricao != "Fazenda");
}
if (!string.IsNullOrEmpty(ufFazenda) ||
!string.IsNullOrEmpty(cidadeFazenda))
{
EnderecoProspect endPros1 = null;
TipoEndereco tipoEnd1 = null;
query
.JoinQueryOver<EnderecoProspect>(x => x.Enderecos,()=> endPros1)
.And(()=> endPros1.Uf ==ufFazenda)
.JoinQueryOver<TipoEndereco>(x => x.TipoEndereco,()=> tipoEnd1)
.And(()=> tipoEnd1.Descricao == "Fazenda");
}
When I try to run I get the message that the path is duplicated. Im using alias correct? what problem? havy ideal? exception is "duplicate association path"
I managed to solve with LINQ to NHibernate ... there is the example for all ...
var q =
from c in Context.Query<Prospect>()
join o in Context.Query<EnderecoProspect>() on c.Identificacao equals o.Prospect.Identificacao
join e in Context.Query<TipoEndereco>() on o.TipoEndereco.Identificacao equals e.Identificacao
join a in Context.Query<EnderecoProspect>() on c.Identificacao equals a.Prospect.Identificacao
join b in Context.Query<TipoEndereco>() on a.TipoEndereco.Identificacao equals b.Identificacao
where (
(
(o.Uf == ufFazenda || ufFazenda == null) &&
(o.Cidade == cidadeFazenda || cidadeFazenda == null)
) && e.Descricao == "Fazenda"
)
&&
(
(
(a.Uf == ufResidencia || ufResidencia == null) &&
(a.Cidade == cidadeResidencia || cidadeResidencia == null)
) && b.Descricao != "Fazenda"
)
Now I can sleep a little more until ...ehehehe...see you