Linq to entities outer join, then an inner join - linq

i have this code:
var query = (from p in dc.GetTable<Person>()
join pa in dc.GetTable<PersonAddress>() on p.Id equals pa.PersonId into tempAddresses
from addresses in tempAddresses.DefaultIfEmpty()
select new { p.FirstName, p.LastName, addresses.State });
this works good, as a outer join for persons with their (optional) address.
Now i want to join addresstype to this query with an inner join, because every address has exactly one addresstype.
So for every person, get the address, and if it has an address, also get the addresstype.
So i created this (added the inner join to addresstype):
var query = (from p in dc.GetTable<Person>()
join pa in dc.GetTable<PersonAddress>() on p.Id equals pa.PersonId into tempAddresses
from addresses in tempAddresses.DefaultIfEmpty()
join at in dc.GetTable<AddressTypes>() on pa.addresstypeid equals at.addresstypeid
select new { p.FirstName, p.LastName, addresses.State, at.addresstype });
Now i get an null reference error on the pa.addresstypeid when the person has no address....
Is there a way to create this in linq?
The above code is behavioural exactly like my code, but my code has different entities but i'm not allowed to show actual code...
EDIT:
here is an example from my code which actually works: (replace the tables with foo/bar)
from foo in foos
join bar in new barRepository(SessionTicket).GetList()
on foo.barId equals bar.barId
join barpersonbar in new barPersonbarRepository(SessionTicket,personId).GetList().Where(z=>z.PersonId == personid)
on bar.barId equals barpersonbar.barId
into outerbarpersonbar
from barpersonbar in outerbarpersonbar.DefaultIfEmpty()
Isn't that the same as in your comment, that 'pa' is out of scope, isn't that the 'bar' in this code

pa isnt in scope once you use the into
change
on pa.addresstypeid equals
to
on addresses.addresstypeid equals

Related

Linq query not returning expected results even when using DefaultIfEmpty

I have the following query in one of my Entity Framework Core API controllers:
var plotData = await (from nl in _context.BookList
join ql in _context.PlotList on nl.PlotId equals ql.PlotId
join qc in _context.PlotChoices on ql.PlotId equals qc.PlotId
join nk in _context.BookLinks.DefaultIfEmpty() on qc.ChoiceId equals nk.ChoiceId
where nl.Id == ID
select new
{ .. }
I need it to return all rows even if data doesn't exist in the BookLinks table.
However, it's not returning rows if there is no data data in the BookLinks table for that row.
But this SQL query, from which I'm trying to model from, does return data...it returns nulls if there is no data in BookLinks.
select * from BookList bl
left join PlotList pl ON bl.plotId = bl.plotId
left join PlotChoices pc ON pl.plotId = pc.plotId
left join BookLinks bk ON pc.choiceID = bk.choiceID
where nl.caseID = '2abv1'
From what I read online, adding 'DefaultIfEmpty()' to the end of BookLinks should fix that, but it hasn't.
What am I doing wrong?
Thanks!
When using left join , you can try below code sample :
var plotData = (from nl in _context.BookList
join ql in _context.PlotList on nl.PlotId equals ql.PlotId
join qc in _context.PlotChoices on ql.PlotId equals qc.PlotId
join nk in _context.BookLinks on qc.ChoiceId equals nk.ChoiceId into Details
from m in Details.DefaultIfEmpty()
where nl.Id == ID
select new
{
}).ToList();

complex t-sql to linq query: inner join, group by, select

I'm trying to build a linq query based on this:
select
SERVICE_REQUEST_CR.SRCR_FK_SR, SERVICE_REQUEST.SR_TX_NAME,
AC_USER.USER_TX_NAME, SERVICE_REQUEST_CR.SRCR_DT_CREATED,
SERVICE_REQUEST_CR_STATUS.SRCRST_TX_DESCRIPTION,
COUNT(SERVICE_REQUEST_PROGRAM.SRPG_FK_SR_ID) as Activities
from
SERVICE_REQUEST_CR
inner join
AC_USER on AC_USER.USER_ID = SERVICE_REQUEST_CR.SRCR_FK_REQUESTOR
inner join
SERVICE_REQUEST_CR_STATUS on SERVICE_REQUEST_CR_STATUS.SRCRST_ID = SERVICE_REQUEST_CR.SRCR_FK_CR_STATUS
inner join
SERVICE_REQUEST on SERVICE_REQUEST.SR_ID = SERVICE_REQUEST_CR.SRCR_FK_SR
inner join
SERVICE_REQUEST_PROGRAM on SERVICE_REQUEST_PROGRAM.SRPG_FK_SR_ID = SERVICE_REQUEST_CR.SRCR_FK_SR
group by
SERVICE_REQUEST_CR.SRCR_FK_SR, SERVICE_REQUEST.SR_TX_NAME,
AC_USER.USER_TX_NAME, SERVICE_REQUEST_CR.SRCR_DT_CREATED,
SERVICE_REQUEST_CR_STATUS.SRCRST_TX_DESCRIPTION,
SERVICE_REQUEST_PROGRAM.SRPG_FK_SR_ID
This is as far as I could come up with:
Dim x = From cr In db.SERVICE_REQUEST_CR
Join usr In db.AC_USER On usr.USER_ID Equals cr.SRCR_FK_REQUESTOR
Join crSt In db.SERVICE_REQUEST_CR_STATUS On crSt.SRCRST_ID Equals cr.SRCR_FK_CR_STATUS
Join sr In db.SERVICE_REQUEST On sr.SR_ID Equals cr.SRCR_FK_SR
Join srProg In db.SERVICE_REQUEST_PROGRAM On srProg.SRPG_FK_SR_ID Equals cr.SRCR_FK_SR
Could anyone give me a help with this? It's the grouping that gets confusing so I just put the joins and the query to keep it simple.
Thanks,
Something like this, but I am not sure about Basic syntax:
Dim x = From cr In db.SERVICE_REQUEST_CR
Join usr In db.AC_USER On usr.USER_ID Equals cr.SRCR_FK_REQUESTOR
Join crSt In db.SERVICE_REQUEST_CR_STATUS On crSt.SRCRST_ID Equals cr.SRCR_FK_CR_STATUS
Join sr In db.SERVICE_REQUEST On sr.SR_ID Equals cr.SRCR_FK_SR
Join srProg In db.SERVICE_REQUEST_PROGRAM On srProg.SRPG_FK_SR_ID Equals cr.SRCR_FK_SR
group new
{
cr.SRCR_FK_SR,
sr.SR_TX_NAME,
usr.USER_TX_NAME,
cr.SRCR_DT_CREATED,
crSt.SRCRST_TX_DESCRIPTION,
srProg.SRPG_FK_SR_ID
}
by new
{
cr.SRCR_FK_SR,
sr.SR_TX_NAME,
usr.USER_TX_NAME,
cr.SRCR_DT_CREATED,
crSt.SRCRST_TX_DESCRIPTION,
srProg.SRPG_FK_SR_ID
} into gr
select new
{
gr.Key.SRCR_FK_SR,
gr.Key.SR_TX_NAME,
gr.Key.USER_TX_NAME,
gr.Key.SRCR_DT_CREATED,
gr.Key.SRCRST_TX_DESCRIPTION,
gr.Key.SRPG_FK_SR_ID,
Activities = gr.Count()
}

Linq left outer group by, then left outer the group

I've this query that i'm trying to put as linq:
select *
from stuff
inner join stuffowner so on so.stuffID = stuff.stuffID
left outer join (select min(loanId) as loanId, stuffownerId from loan
where userid = 1 and status <> 2 group by stuffownerId) t on t.stuffownerid = so.stuffownerid
left outer join loan on t.LoanId = loan.LoanId
when this is done, I would like to do a linq Group by to have Stuff has key, then stuffowners + Loan as value.
I can't seem to get to a nice query without sub query (hence the double left outer).
So basically what my query does, is for each stuff I've in my database, bring the owners, and then i want to bring the first loan a user has made on that stuff.
I've tried various linq:
from stuff in Stuffs
join so in StuffOwners on stuff.StuffId equals so.StuffId
join tLoan in Loans on so.StuffOwnerId equals tLoan.StuffOwnerId into tmpJoin
from tTmpJoin in tmpJoin.DefaultIfEmpty()
group tTmpJoin by new {stuff} into grouped
select new {grouped, fluk = (int?)grouped.Max(w=> w.Status )}
This is not good because if I don't get stuff owner and on top of that it seems to generate a lot of queries (LinqPad)
from stuff in Stuffs
join so in StuffOwners on stuff.StuffId equals so.StuffId
join tmpLoan in
(from tLoan in Loans group tLoan by tLoan.StuffOwnerId into g
select new {StuffOwnerId = g.Key, loanid = (from t2 in g select t2.LoanId).Max()})
on so.StuffOwnerId equals tmpLoan.StuffOwnerId
into tmptmp from tMaxLoan in tmptmp.DefaultIfEmpty()
select new {stuff, so, tmptmp}
Seems to generate a lot of subqueries as well.
I've tried the let keyworkd with:
from tstuffOwner in StuffOwners
let tloan = Loans.Where(p2 => tstuffOwner.StuffOwnerId == p2.StuffOwnerId).FirstOrDefault()
select new { qsdq = tstuffOwner, qsdsq= (int?) tloan.Status, kwk= (int?) tloan.UserId, kiwk= tloan.ReturnDate }
but the more info i get from tLoan, the longer the query gets with more subqueries
What would be the best way to achieve this?
Thanks

How to use let with this LINQ query?

Here is my LINQ query with multiple joins:
it is working good but I need to do an enhancement in its working.
var selectedResults=
from InvoiceSet in Invoices
join BookedAreaSet in BookedAreas on InvoiceSet.InvoiceID equals BookedAreaSet.InvoiceID
join AreaSet in Areas on BookedAreaSet.AreaID equals AreaSet.AreaID
join ContactSet in Contacts on InvoiceSet.ContactID equals ContactSet.ContactID
join Contacts_ObjectsSet in Contacts_Objects on ContactSet.ContactID equals Contacts_ObjectsSet.ContactID
join CompanySet in Companies on Contacts_ObjectsSet.ObjectReferenceID equals CompanySet.CompanyID
join Customer_CustomerGroupSet in Customer_CustomerGroup on Contacts_ObjectsSet.ObjectReferenceID equals Customer_CustomerGroupSet.CustomerID
join CustomerGroupDiscountsSet in CustomerGroupDiscounts on Customer_CustomerGroupSet.CustomerGroupID equals CustomerGroupDiscountsSet.ID
join InvoiceStatusSet in InvoiceStatus on InvoiceSet.InvoiceStatusID equals InvoiceStatusSet.ID
where Contacts_ObjectsSet.ObjectReference=="Company"
//let minDate=(BookedAreaSet.LeasedDate).Min() where BookedAreaSet.InvoiceID=InvoiceSet.InvoiceID
select new {licensee=(CompanySet.CompanyName),CustomerGroupDiscountsSet.CustomerGroup,AreaSet.Location,InvoiceSet.InvoiceNumber,InvoiceSet.Amount,InvoiceSet.TotalDiscount,InvoiceSet.GST,
Paid=(InvoiceSet.InvoiceStatusID==2 ? "Paid":"UnPaid"),
datePaid=(InvoiceSet.PaymentDate),InvoiceSet.PaymentDate//,miDate
};
In query I have commented what I want to add as well as commented in Select. From BookedArea table I want to get Minimum Leased Date for every invoiceID.
I just have started using LINQ so dont know how to do this.
Please guide me.
Thanks
Try this:
var selectedResults=
from InvoiceSet in Invoices
join BookedAreaSet in BookedAreas on InvoiceSet.InvoiceID equals BookedAreaSet.InvoiceID
join AreaSet in Areas on BookedAreaSet.AreaID equals AreaSet.AreaID
join ContactSet in Contacts on InvoiceSet.ContactID equals ContactSet.ContactID
join Contacts_ObjectsSet in Contacts_Objects on ContactSet.ContactID equals Contacts_ObjectsSet.ContactID
join CompanySet in Companies on Contacts_ObjectsSet.ObjectReferenceID equals CompanySet.CompanyID
join Customer_CustomerGroupSet in Customer_CustomerGroup on Contacts_ObjectsSet.ObjectReferenceID equals Customer_CustomerGroupSet.CustomerID
join CustomerGroupDiscountsSet in CustomerGroupDiscounts on Customer_CustomerGroupSet.CustomerGroupID equals CustomerGroupDiscountsSet.ID
join InvoiceStatusSet in InvoiceStatus on InvoiceSet.InvoiceStatusID equals InvoiceStatusSet.ID
where Contacts_ObjectsSet.ObjectReference == "Company"
join BookedAreaSet2 in BookedAreas on InvoiceSet.InvoiceID equals BookedAreaSet.InvoiceID into BookedAreas2
let minDate = BookedAreas2.Select(ba2 => ba2.LeasedDate).Min()
select new
{
licensee = CompanySet.CompanyName,
CustomerGroupDiscountsSet.CustomerGroup,
AreaSet.Location,
InvoiceSet.InvoiceNumber,
InvoiceSet.Amount,
InvoiceSet.TotalDiscount,
InvoiceSet.GST,
Paid = InvoiceSet.InvoiceStatusID == 2 ? "Paid" : "UnPaid",
datePaid = InvoiceSet.PaymentDate,
InvoiceSet.PaymentDate,
minDate,
};

Joining three tables and using a left outer join

I have three tables. Two of them join equally but one will need to join with a left. I'm finding a lot of code to do this in linq but between two tables only.
Here is the SQL code that I'm trying to re-code within LINQ.
SELECT PRSN.NAME
,CO.NAME
,PROD.NAME
FROM PERSON PRSN
INNER JOIN COMPANY CO ON PRSN.PERSON_ID = CO.PERSON_ID
LEFT OUTER JOIN PRODUCT PROD ON PROD.PERSON_ID = PROD.PERSON_ID;
Here is a snippet of LINQ code that I'm using as a base. I'm just not able to piece together the third table (product in my sample SQL) via LINQ and with a left outer join. The sample is between two tables. Thanks for any tips.
var leftOuterJoinQuery =
from category in categories
join prod in products on category.ID equals prod.CategoryID into prodGroup
from item in prodGroup.DefaultIfEmpty(new Product{Name = String.Empty, CategoryID = 0})
select new { CatName = category.Name, ProdName = item.Name };
Michael
How about this:
var loj = (from prsn in db.People
join co in db.Companies on prsn.Person_ID equals co.Person_ID
join prod in db.Products on prsn.Person_ID equals prod.Person_ID into prods
from x in prods.DefaultIfEmpty()
select new { Person = prsn.NAME, Company = co.NAME, Product = x.NAME })
EDIT: if you want to do a left outer join on all tables, you can do it like this:
var loj = (from prsn in db.People
join co in db.Companies on prsn.Person_ID equals co.Person_ID into comps
from y in comps.DefaultIfEmpty()
join prod in db.Products on prsn.Person_ID equals prod.Person_ID into prods
from x in prods.DefaultIfEmpty()
select new { Person = prsn.NAME, Company = y.NAME, Product = x.NAME })
Taken from another Stackoverflow thread somewhere, there's a more legible way to do this:
var loj = (from prsn in db.People
from co in db.Companies.Where(co => co.Person_ID == prsn.Person_ID).DefaultIfEmpty()
from prod in db.Products.Where(prod => prod.Person_ID == prsn.Person_ID).DefaultIfEmpty()
select new { Person = prsn.NAME, Company = co.NAME, Product = prod.NAME })
This uses a mix of linq query syntax and lambda syntax to what (I believe is) the best result. There's no copious re-aliasing of identifiers, and it's the most concise way to do this that I've seen.

Resources