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;
Related
following query is throwing error "The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'GroupJoin"
from ph in context.PrPayfileHeaders
join pp in context.PrPayfileParticipants on ph.PayfileHeaderId equals pp.PayfileHeaderId
join sec in context.SecUsers on pp.AnalystUserId equals sec.UserId
join hp in context.HcmPerson on // Error statement
new
{
key1 = pp.ParticipantEmpNumber,
key2 = ph.PayPeriod
}
equals
new
{
key1 = hp.EmployeeNumber,
key2 = hp.PeriodId
}
into fg
from fgi in fg.DefaultIfEmpty()
where ph.PayfileType == payFileType && ph.IntfControlId == intf_Control_Id && (region == null || ph.RegionName == region)
select new FinalPayFileInfo
{
Participant_id = pp.PayfileParticipantId,
Home_country = fgi.HomeCountry,
Region_name = ph.RegionName,
Analyst_name = sec.FirstName + " " + sec.LastName,
Full_name = fgi.FullName,
Department = pp.DeptNumber,
Cost_center = pp.CostCenter,
Local_currency = pp.LocalCurrency,
Comm_attribute1 = pp.CommAttribute1,
Comm_attribute2 = pp.CommAttribute2,
Comm_attribute3 = pp.CommAttribute3,
Comm_attribute4 = pp.CommAttribute4,
Comm_attribute5 = pp.CommAttribute5,
Comm_attribute6 = pp.CommAttribute6,
Comm_attribute7 = pp.CommAttribute7
};
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
}
));
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;
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.
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()