So I had an issue yesterday that I worked through and increased the speed of my program, but I'm having an issue where I never hit the return breakpoint nor an exception. This query can run multiple times at once depending on the schedualed times it needs to run, but does work if I run it solo with one company. Any idea why?
try
{
//work on query further , need to get client ID correctly
if (vehicleses.Count == 0)
return null;
string siteId = QueryExportSiteWithId(exportSiteId).SiteId;
string clientId = vehicleses[0].ClientID;
var inventoryyReturn = from i in db.Inventory_Vehicles
where dealerIdList.Any(c => c == i.ClientID) && i.Archived != "1" && i.Holding != "1" &&
i.Status == "A"
select i;
var inventoryDescription = from i in db.Inventory_Descriptions
where
inventoryyReturn.Any(
c => new {client = c.ClientID, inv = c.ID} == new {client = i.ClientID, inv = i.InventoryID})
select i;
var settingsExports = from i in db.Settings_Exports
where inventoryyReturn.Any(c => c.ClientID == i.ClientID)
select i;
var settingLots = from i in db.Settings_Lots
where
inventoryyReturn.Any(
c => new {client = c.ClientID, lot = c.LotID} == new {client = i.ClientID, lot = i.ID})
select i;
var inventoryFeatures = from i in db.Inventory_Features
where
inventoryyReturn.Any(
c => new {client = c.ClientID, inv = c.ID} == new {client = i.ClientID, inv = i.InventoryID})
select i;
var inventoryPhotos = from i in db.Inventory_Photos
where
inventoryyReturn.Any(c => c.ID == i.InventoryID)
select i;
var longReturnListt = await (from i in inventoryyReturn
join l in inventoryDescription on new {inv = i.ID, cli = i.ClientID} equals
new {inv = l.InventoryID, cli = l.ClientID} into descGroup
from m in descGroup.DefaultIfEmpty()
join s in settingsExports on i.ClientID equals s.ClientID into settingGroup
from sg in settingGroup.DefaultIfEmpty()
join sl in settingLots on new {client = sg.ClientID, lot = sg.LotID} equals
new {client = sl.ClientID, lot = sl.ID} into lotsGroup
from lg in lotsGroup.DefaultIfEmpty()
join se in db.Settings_ExportSites on new {client = lg.ClientID, lotId = i.LotID, site = siteId}
equals new {client = se.ClientID, lotId = se.LotID, site = se.Site} into exportGroup
from eg in exportGroup.DefaultIfEmpty()
join f in inventoryFeatures on new {inv = i.ID, cli = i.ClientID} equals
new {inv = f.InventoryID, cli = f.ClientID} into invFeatGroup
from ifg in invFeatGroup.DefaultIfEmpty()
join p in inventoryPhotos on i.ID equals p.InventoryID into photo
from photos in photo.DefaultIfEmpty()
orderby i.ID
select new {i, m, sg, photoo = photo.ToList(), lg, ifg, eg}
into grouped
// group grouped by new {grouped.i.ClientID } into groupedDone
// select groupedDone
select new NewType
{
InventoryVehicles = grouped.i,
InventoryDescriptions = grouped.m,
SettingsExports = grouped.sg,
InventoryPhotos = grouped.photoo,
SettingsLots = grouped.lg,
InventoryFeatures = grouped.ifg,
SettingsExportSites = grouped.eg
}
).AsNoTracking().ToListAsync();
if (longReturnListt != null) returnList.AddRange(longReturnListt);
}
catch (Exception exception)
{
Console.WriteLine(exception);
throw;
}
return returnList;
Related
public ActionResult EditArticle(int id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var typeId = 0;
var catId = 0;
var subCatId = 0;
var viewModel = (from sa in ems.SupportArticles
join ssc in ems.SupportSubCategories on sa.SubCatID equals ssc.SubCatID
join sc in ems.SupportCategories on ssc.CatID equals sc.CatID
join st in ems.SupportTypes on sc.TypeID equals st.TypeID
where sa.ArcticleId == id
select new SupportArticleViewModel { supportArticle = sa, supportSubCat = ssc, supportCat = sc, supportType = st });
foreach (var vm in viewModel)
{
typeId = vm.supportType.TypeID;
catId = vm.supportCat.CatID;
subCatId = vm.supportSubCat.SubCatID;
}
I want to convert it into Lambda Notation.But, I am unable to do it.Please help.I am using SupportViewModel which contains property of SupportType,SupportCategory ,SupportSubCategoryand SupportArticle.
Following is functional way do query , you have to make use of join function and than you get data
var filteredArtciles = SupportArticles.Where(sa=> sa.ArcticleId == id);
var query =
SupportArticles.
Join(SupportSubCategories,sa => sa.SubCatID ,ssc => ssc.SubCatID,(sa, ssc) => new {sa,ssc}).
Join(SupportCategories,sassc => sassc.ssc.CatID ,sc=>sc.CatID ,(sassc, sc) => new {sassc,sc}).;
Join(SupportTypes,sasscsc => sasscsc.sc.TypeID ,st=>st.TypeID ,(sc, st) => new {sasscsc,st}).
Select(j=>
new SupportArticleViewModel
{
supportArticle = j.sasscsc.sassc.sa,
supportSubCat = j.sasscsc.sassc.ssc,
supportCat = j.sasscsc.sc,
supportType = j.st
}
));
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();
I am working on asp.net core application. I have following linq query which is being used to fill a model. This model has a property qtryregions which is a list. How can I fill it in one query.
public JsonResult PODetailsList(int id)
{
List<PODetailsFull> Products;
Products =
(from d in db.PODetails
join p in db.Products on d.ProductID equals p.ProductID
where d.POID == id
select new PODetailsFull
{
PODetailID = d.PODetailID,
//QtyRegion = new List<string>() { Region.Region + " " + POAllocation.Allocate != null ? POAllocation.Allocate.ToString() : "" },
Sku = p.Sku,
Cntnr20 = p.Cntnr20 ?? 0,
Cntnr40 = p.Cntnr40 ?? 0,
Cntnr40HQ = p.Cntnr40HQ ?? 0,
}).ToList();
foreach (var product in Products)
{
var result = (from POAllocation in db.POAllocations.Where(p => p.PODetailID == product.PODetailID)
join regions in db.Regions on POAllocation.RegionID equals regions.RegionID
select regions.Region + " " + POAllocation.Allocate ?? ""
).ToList();
product.QtyRegion = result;
}
return Json(new { data = Products }, JsonRequestBehavior.AllowGet);
}
I don't want to use above foreach and want to populate product.Qtyregion in first linq query.
Please suggest.
Is this what you want?
Products = (
from d in db.PODetails
join p in db.Products on d.ProductID equals p.ProductID
where d.POID == id
select new PODetailsFull
{
PODetailID = d.PODetailID,
//QtyRegion = new List<string>() { Region.Region + " " + POAllocation.Allocate != null ? POAllocation.Allocate.ToString() : "" },
Sku = p.Sku,
Cntnr20 = p.Cntnr20 ?? 0,
Cntnr40 = p.Cntnr40 ?? 0,
Cntnr40HQ = p.Cntnr40HQ ?? 0,
QtyRegion = (
from POAllocation in db.POAllocations.Where(p => p.PODetailID == d.PODetailID)
join regions in db.Regions on POAllocation.RegionID equals regions.RegionID
select regions.Region + " " + POAllocation.Allocate ?? "").ToList(),
}).ToList();
Try to use Group By to avoid N+1 queries issue:
Products = (
from d in db.PODetails
join p in db.Products on d.ProductID equals p.ProductID
join palloc in db.POAllocations on d.PODetailID equals palloc.PODetailID
group by new {
PODetailID = d.PODetailID,
Sku = p.Sku,
Cntnr20 = p.Cntnr20,
Cntnr40 = p.Cntnr40,
Cntnr40HQ = p.Cntnr40HQ }
into grp
where grp.Key.POID == id
select new PODetailsFull
{
PODetailID = grp.Key.PODetailID,
QtyRegion = grp,
Sku = grp.Key.Sku,
Cntnr20 = grp.Key.Cntnr20 ?? 0,
Cntnr40 = grp.Key.Cntnr40 ?? 0,
Cntnr40HQ = grp.Key.Cntnr40HQ ?? 0
}).ToList();
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()
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;