LINQ to Entities does not recognize the method 'Int32 ToInt32(Boolean)' - c#

While (Goster) field type is integer on Genel_KullaniciMenu context. This query is working. But I changed the type of that field to bit on the database and entity class. then query is not working. It gives error like "LINQ to Entities does not recognize the method 'Int32 ToInt32(Boolean)'". What may i change on this query? Thanks for your helps.
var query = (from K in
(
(from Genel_Form in _context.Form
join Genel_MenuNesne in _context.MenuNesne on new { GenelFormID = Genel_Form.GenelFormID } equals new { GenelFormID = (Genel_MenuNesne.GenelFormID) } into Genel_MenuNesne_join
from Genel_MenuNesne in Genel_MenuNesne_join.DefaultIfEmpty()
join Genel_KullaniciMenu in _context.KullaniciMenu on new { GenelMenuNesneID = Genel_MenuNesne.GenelMenuNesneID } equals new { GenelMenuNesneID = (Genel_KullaniciMenu.GenelMenuNesneID) } into Genel_KullaniciMenu_join
from Genel_KullaniciMenu in Genel_KullaniciMenu_join.DefaultIfEmpty()
join Genel_FormTuru in _context.FormTuru on Genel_Form.GenelFormTuruID equals Genel_FormTuru.GenelFormTuruID into Genel_FormTuru_join
from Genel_FormTuru in Genel_FormTuru_join.DefaultIfEmpty()
join Genel_MenuGrup in _context.MenuGrup on new { GenelMenuGrupID = (Genel_MenuNesne.GenelMenuGrupID) } equals new { GenelMenuGrupID = Genel_MenuGrup.GenelMenuGrupID } into Genel_MenuGrup_join
from Genel_MenuGrup in Genel_MenuGrup_join.DefaultIfEmpty()
join Genel_KullaniciRol in _context.KullaniciRol on new { GenelKullaniciID = (Genel_KullaniciMenu.GenelKullaniciID) } equals new { GenelKullaniciID = Genel_KullaniciRol.GenelKullaniciID } into Genel_KullaniciRol_join
from Genel_KullaniciRol in Genel_KullaniciRol_join.DefaultIfEmpty()
where
!Genel_FormTuru.FormTuruAdi.Contains("Mobile Ekranlar") &&
(from Genel_RolGrup in _context.RolGrup
where
(from Genel_KullaniciRol0 in _context.KullaniciRol
where
Genel_KullaniciRol0.GenelKullaniciID == genelKullaniciID
select new
{
Genel_KullaniciRol0.GenelRolID
}).Contains(new { GenelRolID = Genel_RolGrup.GenelRolID })
select new
{
Genel_RolGrup.GenelGrupID
}).Contains(new { GenelGrupID = Genel_KullaniciMenu.GenelGrupID })
select new
{
MenuGrupAdi = (Genel_MenuGrup.MenuGrupAdi + "-" + Genel_FormTuru.FormTuruAdi.ToUpper()),
Baslik = (Genel_Form.FormKodu + " " + Genel_MenuNesne.MenuNesneAdi),
Genel_Form.FormNameSpaceAdi,
Genel_Form.FormClassAdi,
Goster = Genel_KullaniciMenu.Goster,
GenelGrupID = Genel_KullaniciMenu.GenelGrupID,
GenelRolID = Genel_KullaniciRol.GenelRolID,
GenelMenuGrupID = Genel_MenuNesne.GenelMenuGrupID,
GenelFormTuruID = Genel_Form.GenelFormTuruID,
GenelMenuNesneID = Genel_MenuNesne.GenelMenuNesneID
}))
group K by new
{
K.GenelRolID,
K.MenuGrupAdi,
K.Baslik,
K.FormNameSpaceAdi,
K.FormClassAdi,
K.GenelMenuGrupID,
K.GenelFormTuruID,
K.GenelMenuNesneID
} into g
orderby
g.Key.GenelMenuGrupID,
g.Key.GenelFormTuruID,
g.Key.GenelMenuNesneID
select new
{
g.Key.MenuGrupAdi,
g.Key.Baslik,
g.Key.FormNameSpaceAdi,
g.Key.FormClassAdi,
Goster = g.Max(p => p.Goster),
}).ToList()

Related

Entity Framework Core - (Kind of subquery)

I use fluent API and Ef Core.
This is my linq:
var data = (from candidateHP in _cempContexto.CandidateHiringProcessSummary
join jobsHP in _cempContexto.JobHiringProcessSummary on candidateHP.JobCode equals jobsHP.JobCode
join job in _cempContexto.Vaga on jobsHP.JobCode equals job.Codigo
join candidateJob in _cempContexto.TrCandidatoVaga on new { X = job.Codigo, Y = candidateCode } equals new { X = candidateJob.VagaCodigo, Y = candidateJob.CandidatoCodigo }
where
candidateHP.CandidateCode == candidateCode
select
new
{
JobCode = jobsHP.JobCode,
CompanyCode = job.EmpresaCodigo,
Title = job.Titulo,
HasImage = true,
CompanyName = job.NomeEmpresa,
QuantityJobs = job.QuantidadeVaga,
Location = job.CidadeCodigo,
QuantityResumeSent = jobsHP.QuantityResumeSent,
JobStatusCode = jobsHP.StatusCode,
CandidateStatusCode = candidateHP.StatusCode,
StatusCode = job.StatusCodigo,
ResumeSentDate = candidateJob.Data,
InsertDate = job.DataDeCadastro,
EndDate = job.DataDeSaida,
city= _cempContexto.TrVagaCidade.SelectMany(p => _cempContexto.TrVagaCidade.Where(q => q.VagaCodigo == candidateHP.JobCode).Select(q => new { q })).ToList()
})
.ToList();
I need to Fill city with that subquery. I need a better way to do that, How could I proceed?
Split code into two queries :
var query = (from candidateHP in _cempContexto.CandidateHiringProcessSummary
join jobsHP in _cempContexto.JobHiringProcessSummary on candidateHP.JobCode equals jobsHP.JobCode
join job in _cempContexto.Vaga on jobsHP.JobCode equals job.Codigo
join candidateJob in _cempContexto.TrCandidatoVaga on new { X = job.Codigo, Y = candidateCode } equals new { X = candidateJob.VagaCodigo, Y = candidateJob.CandidatoCodigo }
where
candidateHP.CandidateCode == candidateCode
select
new
{ jobsHP = jobsHP, job = job, candidateJob = candidateJob}).ToList();
var data = query.Select(x => new {
JobCode = x.jobsHP.JobCode,
CompanyCode = x.job.EmpresaCodigo,
Title = x.job.Titulo,
HasImage = true,
CompanyName = x.job.NomeEmpresa,
QuantityJobs = x.job.QuantidadeVaga,
Location = x.job.CidadeCodigo,
QuantityResumeSent = x.jobsHP.QuantityResumeSent,
JobStatusCode = x.jobsHP.StatusCode,
CandidateStatusCode = x.candidateHP.StatusCode,
StatusCode = x.job.StatusCodigo,
ResumeSentDate = x.candidateJob.Data,
InsertDate = x.job.DataDeCadastro,
EndDate = x.job.DataDeSaida,
city= query.Select(y => y.candidateHP.JobCode).Select(q => new { q })).ToList()
})
.ToList();

The cast to value type 'System.Int32' failed because the materialized value is null.

I'm trying to return a list of results, however. Whenever there are no results, I receive the error message which I have posted above. However, it's strange because whenever I add the variable q instead of the return, it just returns no results instead and is fine with this. I would prefer to do it the way which I am currently doing it right now, does anybody know what is wrong with the query? Whenever I run it in LINQPad it works completely fine.
public IQueryable<ClaimNumberReport> GetClaimsByClaimNumber(int ClientID, int ClaimID) {
/*var q = */ return (from d in camOnlineDb.Details
join a in camOnlineDb.Areas
on new { a = d.ClientID, b = d.AreaID ?? 0 }
equals new { a = a.ClientID, b = a.AreaID }
where d.ClientID == ClientID
join r in camOnlineDb.Reasons
on new { a = d.ClientID, b = d.ReasonID ?? 0 }
equals new { a = r.ClientID, b = r.ReasonID }
join sd in camOnlineDb.SuppDepts
on new { a = d.ClientID, b = d.CategoryID ?? 0 }
equals new { a = sd.ClientID, b = sd.CategoryID } into sdd
from sd in sdd.DefaultIfEmpty()
join h in camOnlineDb.Headers
on new { d.ClientID, d.ClaimID}
equals new { h.ClientID, h.ClaimID }
where h.ClaimID == ClaimID
join su in camOnlineDb.Suppliers
on new { h.ClientID, h.SupplierID }
equals new {su.ClientID, su.SupplierID }
join cp in camOnlineDb.ClaimPacks
on new { h.ClientID, h.ClaimID }
equals new { cp.ClientID, cp.ClaimID }
join rev in camOnlineDb.Reviews
on new { h.ClientID, h.ReviewID }
equals new { rev.ClientID, rev.ReviewID }
join revp in camOnlineDb.ReviewPeriods
on new { a = rev.ClientID, b = rev.ReviewPeriodID ?? 0 }
equals new { a = revp.ClientID, b = revp.ReviewPeriodID }
join st in camOnlineDb.Statuses
on new { a = d.ClientID, b = d.StatusID ?? 0 }
equals new { a = st.ClientID, b = st.StatusID }
join stcm in camOnlineDb.StatusCategoryMappings
on new { st.ClientID, st.StatusID }
equals new { stcm.ClientID, stcm.StatusID }
join stc in camOnlineDb.StatusCategories
on new { stcm.StatusCategoryID }
equals new { stc.StatusCategoryID }
where stc.StatusCategoryTypeID == 1
select new ClaimNumberReport {
TypeID = d.ClaimTypeID,
CPAttached = cp.FileName,
ReviewPeriodName = revp.ReviewPeriodName,
ClaimID = d.ClaimID,
Line = d.ClaimLine,
AccountNo = su.AccountNo,
SupplierName = su.SupplierName,
Amount = d.Amount,
Status = st.StatusDesc,
DateSent = d.DateSent,
DayOS = d.DaysOS,
NominalPeriod = d.NominalPeriod,
SLInvoiceNo = d.SLInvoiceNo,
Area = a.AreaDesc,
DebitRef = d.DebitFile,
DebitDate = d.JournalDate,
DeductDate = d.DeductDate,
StatusCategoryID = stc.StatusCategoryID,
StatusCategoryDesc = stc.StatusCategoryDesc,
APLReason = r.ReasonDesc,
ClientID = d.ClientID,
DeptNo = sd.DepartmentID,
DeptName = sd.DepartmentName,
Agreed = d.Agreed
});
/*return q;*/
}
This error is caused by a situation where the query result type has a column/property of non-nullable type but the generated query results in a NULL value.
This could be considered a bug or not. It is hard to see what the L2S team should have done differently here. I think they should have added a better error message. This bug is insidious because it sometimes only strikes in production under unusual data...
Your left join (sd) seem not to match and one of the sd.* properties that you select must be an int. Solve that like this:
DeptNo = (int?)sd.DepartmentID, //Cast to nullable
d.CategoryID ?? 0
What are you doing here? This seems to be a way to make the join compile. It's better to use:
join r in camOnlineDb.Reasons
on new { a = d.ClientID, b = (int?)d.ReasonID }
equals new { a = r.ClientID, b = (int?)r.ReasonID }
This cast makes the anonymous type signatures compatible. The generated SQL should now be faster. If you say x ?? 0 that converts to COALESCE(x, 0) which can prevent index use and such.

The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'GroupJoin'

I wanted to get sum of specific columns so I have used the group by in linq, but seems like I must getting the error in join. The datatypes of the fields are the same. This is not the original query but a made up one for Stack Overflow posting.
from T1 in TXPYTRANs
join T11 in ((from p in TXPYTRANs // geting error right here
group p by new { p.TRANTYPE, p.BILLYR, p.BILLNO } into g
select new
{
TRANAMT = g.Sum(b => b.TRANAMT),
TRANPENALTY = g.Sum(b => b.TRANPENALTY),
TRANINTEREST = g.Sum(b => b.TRANINTEREST),
TRANTYPE = g.Select(s => s.TRANTYPE),
BillYear = g.Select(s => s.BILLYR),
BillNumber = g.Select(s => s.BILLNO)
}).Take(100))
on new { BYr = T1.BILLYR, BNo = T1.BILLNO }
equals new { BYr = T11.BillYear, BNo = T11.BillNumber } into T12
from T13 in T12
select new
{
TranType = T13.TRANTYPE,
TranAmt = T13.TRANAMT,
TranPenalty = T13.TRANPENALTY,
TranInterest = T13.TRANINTEREST,
BillYr = T13.BillYr,
BillNo = T13.BillNo
}
You need
BillYear = g.Select(s=>s.BILLYR),
BillNumber = g.Select(s=>s.BILLNO)
to be
BillYear = g.Key.BILLYR,
BillNumber = g.Key.BILLNO
At the moment your Join selectors are
T1 => new { BYr = T1.BILLYR, BNo = T1.BILLNO }
T11 => new { BYr = T11.BillYear, BNo = T11.BillNumber }
and the properties of the second are IEnumerables of the types of the properties of the first, rather than matching the types.
(TRANTYPE should probably be the same.)

Getting a field value from a LINQ query without Iteration

I have the following query in controller and I want to store a column value in a variable but I am not being able to iterate it. Here is my code:
var srmas = (
from SRMAs in db.SRMAs
join SRMAStatus in db.SRMAStatus on SRMAs.Status equals SRMAStatus.Id
join PurchaseOrders in db.PurchaseOrders on SRMAs.PONumber equals PurchaseOrders.PONumber
join Suppliers in db.Suppliers on PurchaseOrders.SupplierID equals Suppliers.SupplierID
join SRMADetails in db.SRMADetails on SRMAs.Id equals SRMADetails.SRMAId
where(SRMAs.Id == srmaid)
group SRMADetails by new
{
SRMADetails.Id,
SRMADetails.SRMAId,
SRMADetails.SupplierPartNum,
SRMAs.PONumber,
SRMAs.ActualAmount,
SRMAs.ApprovedOn,
SRMAs.Status,
SRMAs.TrackingNumber,
SRMAs.SupplierRMANumber,
SRMAs.RequestedFromSupp,
SRMAs.CreatedOn,
Suppliers.SupplierName,
SRMAStatus.StatusName,
PurchaseOrders.PODate,
PurchaseOrders.suppliersOrderNumber
} into grp
select new
{
grp.Key.Status,
grp.Key.SRMAId,
grp.Key.Id,
grp.Key.PONumber,
grp.Key.SupplierRMANumber,
grp.Key.ActualAmount,
grp.Key.SupplierPartNum,
grp.Key.RequestedFromSupp,
grp.Key.TrackingNumber,
grp.Key.ApprovedOn,
grp.Key.SupplierName,
grp.Key.StatusName,
grp.Key.PODate,
grp.Key.suppliersOrderNumber,
grp.Key.CreatedOn,
Sum = grp.Sum(SRMADetails => SRMADetails.Cost * SRMADetails.QtyReturned)
}
).ToList();
System.Collections.IEnumerable et = (System.Collections.IEnumerable)srmas;
IEnumerator it = et.GetEnumerator();
while (it.MoveNext())
{
SRMA current = (SRMA)it.Current;
Response.Write(current.Status);
}
ViewBag.SRMAs = srmas.Select(srma => new IndexViewModel
{
Id = srma.SRMAId,
SupplierRMANum = srma.SupplierRMANumber,
SRMADetailsID = srma.Id,
PONumber = srma.PONumber,
CreatedOn = srma.CreatedOn,
SupplierName = srma.SupplierName,
SRMAStatus = srma.StatusName,
Status = srma.Status,
suppliersOrderNumber = srma.suppliersOrderNumber,
PODate = srma.PODate,
Sum = srma.Sum,
TrackingNumber = srma.TrackingNumber,
ActualAmount = srma.ActualAmount
}).ToList();
I just want to get Status value of first record. How do I do it?

linq join, group by to get count of child table

After getting my join to work I seem to have gotten stuck on the count bit.
What I am attempting below is get a count of documents printed based on the join below.
What would the code be to get the count per 'guardiandocsrequired'?
var guardianEntityType = new {EntityTypeFK = "GUARDIAN"};
return (from d in dbContext.GuardianDocsRequireds
join p in dbContext.DocumentPrintingLogs on
new { docTypeFK = d.DocTypeFK, entityFK = d.GuardianFK } equals
new { docTypeFK = p.DocTypeFK, entityFK = p.EntityFK }
where d.GuardianFK == entityPK && p.ItemGroupFK == itemGroupID && p.EntityTypeFK == "GUARDIAN"
group d by new
{
d.GuardianFK,
d.DocTypeFK,
d.DocumentType.DocTypeDescription,
d.RequiredStatus
}
into res
select new DocumentsRequired
{
EntityPK = res.Key.GuardianFK,
EntityType = entityType,
DocTypeFK = res.Key.DocTypeFK,
DocTypeDescription = res.Key.DocTypeDescription,
RequiredStatus = res.Key.RequiredStatus,
PrintCount = ???
}
).ToList();
If it helps, I have written the sql to produce exactly what I require as follows:
SELECT gdr.DocRequiredID,gdr.RequiredDate,gdr.GuardianFK,gdr.DocTypeFK,gdr.RequiredStatus,
COUNT(dpl.DocPrintedID) AS documentsPrinted
FROM dbo.GuardianDocsRequired gdr
LEFT OUTER JOIN dbo.DocumentPrintingLog dpl ON gdr.DocTypeFK = dpl.DocTypeFK
AND gdr.GuardianFK = dpl.EntityFK
AND dpl.EntityTypeFK = 'GUARDIAN'
WHERE gdr.GuardianFK = #entityPK
GROUP BY gdr.DocRequiredID,gdr.RequiredDate,gdr.GuardianFK,gdr.DocTypeFK,gdr.RequiredStatus
Do you mean sth like this?
var guardiandocsrequired = (from d in dbContext.GuardianDocsRequireds
join p in dbContext.DocumentPrintingLogs on
new { docTypeFK = d.DocTypeFK, entityFK = d.GuardianFK } equals
new { docTypeFK = p.DocTypeFK, entityFK = p.EntityFK }
where d.GuardianFK == entityPK && p.ItemGroupFK == itemGroupID && p.EntityTypeFK == "GUARDIAN"
group d by new
{
d.GuardianFK,
d.DocTypeFK,
d.DocumentType.DocTypeDescription,
d.RequiredStatus
}
into res
select new DocumentsRequired
{
EntityPK = res.Key.GuardianFK,
EntityType = entityType,
DocTypeFK = res.Key.DocTypeFK,
DocTypeDescription = res.Key.DocTypeDescription,
RequiredStatus = res.Key.RequiredStatus,
PrintCount = ???
}
).ToList();
int cnt = guardiandocsrequired.Count;
return guardiandocsrequired;

Categories

Resources