I have a need to perform a complex left join on a table and am unsure how to write the code using a criteria query. Currently I have:
public IList<RezolutionConfig> GetSearchConfigByManagerCategoryProduct(int ManagerId, int ProductTypeId, int ConfigCategoryId)
{
ICriteria criteria = Session.GetISession().CreateCriteria(typeof(RezolutionConfig))
.CreateAlias("RezolutionConfigCategory", "rcc")
.Add(Expression.Eq("rcc.id", ConfigCategoryId))
.CreateAlias("RezolutionProductType","rpt")
.Add(Expression.Eq("rpt.id", ProductTypeId))
.CreateAlias("RezolutionManagerConfigs", "rmc", NHibernate.SqlCommand.JoinType.LeftOuterJoin)
.CreateAlias("rmc.Manager", "m", NHibernate.SqlCommand.JoinType.LeftOuterJoin)
.Add(Expression.Eq("m.id", ManagerId));
return criteria.List<RezolutionConfig>();
}
which produces:
SELECT *
FROM [dbo].[RezolutionConfig] this_
inner join [dbo].[RezolutionConfigCategory] rcc1_ on this_.[RezolutionConfigCategoryId]=rcc1_.[RezolutionConfigCategoryId]
inner join [dbo].[RezolutionProductType] rpt2_ on this_.[RezolutionProductTypeId]=rpt2_.[RezolutionProductTypeId]
left outer join [dbo].[RezolutionManagerConfig] rmc3_ on this_.[RezolutionConfigID]=rmc3_.[RezolutionConfigID]
left outer join [dbo].[Manager] m4_ on rmc3_.[ManagerID]=m4_.[ManagerID] WHERE rcc1_.[RezolutionConfigCategoryId] = 1
and rpt2_.[RezolutionProductTypeId] = 1
and m4_.[ManagerID] = 9135
What I need to produce is
SELECT *
FROM [dbo].[RezolutionConfig] this_
inner join [dbo].[RezolutionConfigCategory] rcc1_ on this_.[RezolutionConfigCategoryId]=rcc1_.[RezolutionConfigCategoryId]
inner join [dbo].[RezolutionProductType] rpt2_ on this_.[RezolutionProductTypeId]=rpt2_.[RezolutionProductTypeId]
left outer join [dbo].[RezolutionManagerConfig] rmc3_ on this_.[RezolutionConfigID]=rmc3_.[RezolutionConfigID]
left outer join [dbo].[Manager] m4_ on rmc3_.[ManagerID]=m4_.[ManagerID] and m4_.[ManagerID] = 9135
WHERE rcc1_.[RezolutionConfigCategoryId] = 1
and rpt2_.[RezolutionProductTypeId] = 1
NHibernate doesn't support adding a constraint to a join as you would like: left outer join [dbo].[Manager] m4_ on rmc3_.[ManagerID]=m4_.[ManagerID] and m4_.[ManagerID] = 9135. I think your best approach will be to write the query in HQL and use a subquery to constrain Manager.
Related
Hoping someone can point me in the right direction with this join. I'm trying to convert some SQL to Linq. My SQL has a left outer join after several inner joins. The following SQL produces the desired result:
SELECT TOP(50) [t].[TagFriendlyName] AS [TagName], [t0].[timeStamp] AS [LastSeen], [l].[Name] AS [LocationName]
FROM [Tags] AS [t]
INNER JOIN [tag_reads] AS [t0] ON [t].[epc] = [t0].[epc]
INNER JOIN [ReaderData] AS [r] ON [t0].[ReaderDataId] = [r].[Id]
LEFT OUTER JOIN [Readers] AS [r0] ON [r].[mac_address] = [r0].[mac_address]
INNER JOIN [Locations] AS [l] on [t0].[antennaPort] = [l].[AntennaId] AND [r].[Id] = [l].[ReaderId]
GROUP BY [t].[TagFriendlyName], [t0].[timeStamp], [l].[Name]
ORDER BY [t0].[timeStamp] DESC
My Linq code is as follows, but I can't figure out how to get the left outer join inserted properly. Not sure how to introduce the Readers table that needs the LEFT OUTER JOIN:
var query = (
from tags in db.Tags
join tagreads in db.tag_reads on tags.epc equals tagreads.epc
join readerdata in db.ReaderData on tagreads.ReaderDataId equals readerdata.Id
join readers in db.Readers on readerdata.mac_address equals readers.mac_address
group tags by new { tags.TagFriendlyName, timestamp = tagreads.timeStamp, readerdata.mac_address } into grp
select new CurrentStatus()
{
TagName = grp.Key.TagFriendlyName,
LastSeen = grp.Key.timestamp,
LocationName = grp.Key.mac_address
}
)
.OrderByDescending(o => o.LastSeen)
According to the documentation I need to use DefaultIfEmpty(), but I'm not sure where to introduce the Readers table.
Using EF Core 3.1.0. THANKS!
You should apply Left Join this way:
join readers in db.Readers on readerdata.mac_address equals readers.mac_address into readersJ
from readers in readersJ.DefaultIfEmpty()
The full code:
var query = (
from tags in db.Tags
join tagreads in db.tag_reads on tags.epc equals tagreads.epc
join readerdata in db.ReaderData on tagreads.ReaderDataId equals readerdata.Id
join readers in db.Readers on readerdata.mac_address equals readers.mac_address into readersJ
from readers in readersJ.DefaultIfEmpty()
join locations in db.Locations
on new { ap = tagreads.antennaPort, rd = readerdata.Id }
equals new { ap = locations.AntennaId, rd = locations.ReaderId }
group tags by new { tags.TagFriendlyName, timestamp = tagreads.timeStamp, readerdata.mac_address } into grp
select new CurrentStatus()
{
TagName = grp.Key.TagFriendlyName,
LastSeen = grp.Key.timestamp,
LocationName = grp.Key.mac_address
}
)
.OrderByDescending(o => o.LastSeen)
I have prepared a SQL query and I'm having problem translating into LINQ query. Below I'm attaching my SQL query.
I have problems with inner join and left join that contains a select inside, and actually a second left join has another select inside a select as well, with group by.
This is really frustrating so maybe you could help me?
select * from Users u
inner join Pickups p on p.Number=u.Number
inner join Revisions r on r.Id=p.RevisionId and r.RevisionText='Done'
inner join (select u2.Id, count(u2.Id) clicks_num
from Users u2
inner join Clicks uc on uc.UserId=u2.Id
group by u2.Id) clicks on clicks.Id=u.Id
left join (select UserId, count(Id) scc
from (select distinct ucx.UserId, ucx.Id
from Clicks ucx
inner join Pickups ucpx on ucpx.Number=ucx.Number
inner join Revisions ucprx on ucprx.Id=ucpx.RevisionId and r.RevisionText='Done') t
group by UserId) s on s.UserId = u.Id
where clicks.clicks_num = s.scc;
I am starting with a simple one, but then could you give me an example how to make an inner select as join?
from u in Context.Set<Users>()
join p in Context.Set<Pickups>() on u.Number equals p.Number
join r in Context.Set<Revisions>() on p.RevisionId equals r.RevisionId
So I have a SQL query that I would like to convert to LINQ.
Here is said query:
SELECT *
FROM DatabaseA.SchemaA.TableA ta
LEFT OUTER JOIN DatabaseA.SchemaA.TableB tb
ON tb.ShipId = ta.ShipId
INNER JOIN DatabaseA.SchemaA.TableC tc
ON tc.PostageId= tb.PostageId
WHERE tc.PostageCode = 'Package'
AND ta.MailId = 'Specification'
The problem I am struggling with is I cannot seem to figure out how to do a left join in LINQ before an inner join, since doing a left join in LINQ is not as clear to me at least.
I have found numerous examples of a LINQ inner join and then a left join, but not left join and then inner join.
If it helps, here is the LINQ query I have been playing around with:
var query = from m in tableA
join s in tableB on m.ShipId equals s.ShipId into queryDetails
from qd in queryDetails.DefaultIfEmpty()
join p in tableC on qd.PostageId equals p.PostageId
where m.MailId == "Specification" && p.PostageCode == "Package"
select m.MailId;
I have tried this a few different ways but I keep getting an "Object reference not set to an instance of an object" error on qd.PostageId.
LINQ is very new to me and I love learning it, so any help on this would be much appreciated. Thanks!
From my SQL conversion recipe:
JOIN conditions that aren't all equality tests with AND must be handled using where clauses outside the join, or with cross product (from ... from ...) and then where
JOIN conditions that are multiple ANDed equality tests between the two tables should be translated into anonymous objects
LEFT JOIN is simulated by using into joinvariable and doing another from from the joinvariable followed by .DefaultIfEmpty().
The order of JOIN clauses doesn't change how you translate them:
var ans = from ta in TableA
join tb in TableB on ta.ShipId equals tb.ShipId into tbj
from tb in tbj.DefaultIfEmpty()
join tc in TableC on tb.PostageId equals tc.PostageId
where tc.PostageCode == "Package" && ta.MailId == "Specification"
select new { ta, tb, tc };
However, because the LEFT JOIN is executed before the INNER JOIN and then the NULL PostageIds in TableB for unmatched rows will never match any row in TableC, it becomes equivalent to an INNER JOIN as well, which translates as:
var ans2 = from ta in tableA
join tb in tableB on ta.ShipId equals tb.ShipId
join tc in tableC on tb.PostageId equals tc.PostageId
where tc.PostageCode == "Package" && ta.MailId == "Specification"
select new { ta, tb, tc };
Use:
var query = from m in tableA
join s in tableB on m.ShipId equals s.ShipId
join p in tableC on s.PostageId equals p.PostageId
where m.MailId == "Specification" && p.PostageCode == "Package"
select m.MailId;
Your query uses a LEFT OUTER JOIN but it doesn't need it.
It will, in practice, function as an INNER JOIN due to your tc.PostageCode = 'Package' clause. If you compare to a column value in a table in a WHERE clause (and there are no OR clauses and you aren't comparing to NULL) then effectively all joins to get to that table will be treated as INNER).
That clause will never be true if TableB is null (which is why you use LEFT OUTER JOIN vs INNER JOIN) - so you should just use an INNER JOIN to make the problem simpler.
I want to count my ReviewDetails.Review column but i got error:
Column 'AdvertiserMaster.AdvertiserID' is invalid in the select list
because it is not contained in either an aggregate function or the
GROUP BY clause.
Here Is My Query
SELECT DISTINCT
AdvertiserMaster.AdvertiserID, AdvertiserMaster.BusinessName , ISNULL(AdvertiserMaster.AverageRating, 0) AS AverageRating, AdvertiserMaster.ImageURL1, AdvertiserMaster.Address1,
AdvertiserMaster.CategoryID, AdvertiserMaster.Email, AdvertiserMaster.CountryID, AdvertiserMaster.StateID, AdvertiserMaster.CityID, AdvertiserMaster.PinCode, AdvertiserMaster.Mobile,
CategoryMaster.CategoryName, CountryMaster.CountryName, StateMaster.StateName, CityMaster.CityName,Count(ReviewDetails.Review) AS ReviewCount
FROM AdvertiserMaster INNER JOIN
BusinessCategoryDetails ON AdvertiserMaster.AdvertiserID = BusinessCategoryDetails.AdvertiserID INNER JOIN
ReviewDetails ON AdvertiserMaster.AdvertiserID = ReviewDetails.AdvertiserID LEFT OUTER JOIN
CategoryMaster ON AdvertiserMaster.CategoryID = CategoryMaster.CategoryID LEFT OUTER JOIN
CountryMaster ON AdvertiserMaster.CountryID = CountryMaster.CountryID LEFT OUTER JOIN
CityMaster ON AdvertiserMaster.CityID = CityMaster.CityID LEFT OUTER JOIN
StateMaster ON AdvertiserMaster.StateID = StateMaster.StateID LEFT OUTER JOIN
SubCategoryMaster ON BusinessCategoryDetails.SubCategoryID = SubCategoryMaster.SubCategoryID
WHERE (AdvertiserMaster.CategoryID = 8) AND (AdvertiserMaster.CityID = 16619) AND (AdvertiserMaster.IsActive = 1)
Since I am trying to get count by writing Count(ReviewDetails.Review)
But it is of no use.
here is my tables:ClassifiedBD Images
If you want to count something (or use other aggregate functions like sum), you need to define the level on which you count with group by clause, for example like this:
SELECT
AdvertiserMaster.AdvertiserID,
AdvertiserMaster.BusinessName,
ISNULL(AdvertiserMaster.AverageRating, 0) AS AverageRating,
AdvertiserMaster.ImageURL1,
AdvertiserMaster.Address1,
AdvertiserMaster.CategoryID,
AdvertiserMaster.Email,
AdvertiserMaster.CountryID,
AdvertiserMaster.StateID,
AdvertiserMaster.CityID,
AdvertiserMaster.PinCode,
AdvertiserMaster.Mobile,
CategoryMaster.CategoryName,
CountryMaster.CountryName,
StateMaster.StateName,
CityMaster.CityName,
COUNT(ReviewDetails.Review) AS ReviewCount
FROM AdvertiserMaster
INNER JOIN BusinessCategoryDetails
ON AdvertiserMaster.AdvertiserID = BusinessCategoryDetails.AdvertiserID
INNER JOIN ReviewDetails
ON AdvertiserMaster.AdvertiserID = ReviewDetails.AdvertiserID
LEFT OUTER JOIN CategoryMaster
ON AdvertiserMaster.CategoryID = CategoryMaster.CategoryID
LEFT OUTER JOIN CountryMaster
ON AdvertiserMaster.CountryID = CountryMaster.CountryID
LEFT OUTER JOIN CityMaster
ON AdvertiserMaster.CityID = CityMaster.CityID
LEFT OUTER JOIN StateMaster
ON AdvertiserMaster.StateID = StateMaster.StateID
LEFT OUTER JOIN SubCategoryMaster
ON BusinessCategoryDetails.SubCategoryID = SubCategoryMaster.SubCategoryID
WHERE (AdvertiserMaster.CategoryID = 8)
AND (AdvertiserMaster.CityID = 16619)
AND (AdvertiserMaster.IsActive = 1)
GROUP BY AdvertiserMaster.AdvertiserID,
AdvertiserMaster.BusinessName,
ISNULL(AdvertiserMaster.AverageRating, 0),
AdvertiserMaster.ImageURL1,
AdvertiserMaster.Address1,
AdvertiserMaster.CategoryID,
AdvertiserMaster.Email,
AdvertiserMaster.CountryID,
AdvertiserMaster.StateID,
AdvertiserMaster.CityID,
AdvertiserMaster.PinCode,
AdvertiserMaster.Mobile,
CategoryMaster.CategoryName,
CountryMaster.CountryName,
StateMaster.StateName,
CityMaster.CityName
You don't need distinct when you have group by.
You also might want to start using aliases for the tables and formatting / indenting your SQL properly
anybody can help me to convert some sql query whit right join like this to linq ?
SELECT dbo.FinnTrans.SanadID, dbo.FinnTrans.Date, dbo.FinnAccount.ID AS AccID,
dbo.FinnAccount.FullId, dbo.FinnAccount.Name, SUM(dbo.FinnTrans.Debit) AS TotalDebit,
SUM(dbo.FinnTrans.Credit) AS TotalCredit
FROM dbo.FinnAccount AS FinnAccount_1 LEFT OUTER JOIN
dbo.FinnAccount ON FinnAccount_1.ParentId = dbo.FinnAccount.ID RIGHT OUTER JOIN
dbo.FinnTrans LEFT OUTER JOIN
dbo.FinnAccount AS FinnAccount_2 ON dbo.FinnTrans.AccID = FinnAccount_2.ID ON
FinnAccount_1.ID = FinnAccount_2.ParentId
WHERE (dbo.FinnTrans.FPID = 7) AND (FinnAccount_2.AccLevel = 3)
GROUP BY dbo.FinnTrans.SanadID, dbo.FinnTrans.Date, dbo.FinnAccount.ID,
dbo.FinnAccount.Name, dbo.FinnAccount.FullId
HAVING (dbo.FinnTrans.SanadID = 1)
You can look here: http://www.hookedonlinq.com/OuterJoinSample.ashx as an example of the left outer join. And you can always swap tables to get either left or right
I've taken the liberty of beatifying your TSQL a little.
The last two join conditions appear malformed to me so this TSQL can not be parsed.
SELECT
[t].SanadID
, [t].Date
, [a].ID [AccID]
, [a].FullId
, [a].Name
, SUM([t].Debit) [TotalDebit]
, SUM([t].Credit) [TotalCredit]
FROM
dbo.FinnAccount [a1]
LEFT OUTER JOIN
dbo.FinnAccount [a]
ON [a1].ParentId = [a].ID
RIGHT OUTER JOIN
dbo.FinnTrans [t]
LEFT OUTER JOIN
dbo.FinnAccount [a2]
ON [a].AccID = [a2].ID
ON [a1].ID = [a2].ParentId
WHERE
[t].FPID = 7
AND
[a2].AccLevel = 3
GROUP BY
[t].SanadID
, [t].Date
, [a].ID
, [a].Name
, [a].FullId
HAVING
[t].SanadID = 1