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()
}
Related
I need to convert a SQL query into LINQ, either to Query Syntax or Method Syntax.
SELECT
IA.ItemId,
IVT.ItemName,
C.DeliveryMethod,
SUM(OD.Quantity) AS Qty
FROM OrderHeader OH
INNER JOIN OrderDetail OD ON OH.OrderId = OD.OrderId
LEFT JOIN Customer C ON OH.CustomerId = C.Id
LEFT JOIN ItemAvailability IA ON OD.ItemId = IA.RecId
INNER JOIN Inventory IVT ON IA.ItemId = IVT.ItemId
WHERE OH.DeliveryDate = '02/03/2023'
AND OH.OrderType = 'Web'
GROUP BY IA.ItemId, Ivt.ItemName, C.DlvMode
ORDER BY IA.ItemId
I've spent a long time Googling and YouTubing, but everyone provides examples of either just Inner Joins, Group Joins, or Left Joins. No one gave example of having both Inner Joins and Left Joins in the same query.
I know the syntax for a simple inner join like this:
SQL
SELECT OH.OrderId, OD.ItemId, OD.Quantity
FROM OrderHeader OH
INNER JOIN OrderDetail OD ON OH.OrderId = OD.OrderId
ORDER BY OD.ItemId
LINQ
from oh in OrderHeader
join od in OrderDetail on oh.OrderId equals od.OrderId
orderby ordt.IItemId
select new {
OrderId = oh.OrderId,
ItemId = od.ItemId,
Quantity = od.Quantity
}
And I know the LINQ syntax for left join also. But I am having trouble to figure out how to have both inner and left joins in one LINQ query.
I hope someone here can help, please.
Just combine join techniques together
var deliveryDate = ...;
var query =
from oh in OrderHeader
join od in OrderDetail on oh.OrderId equals od.OrderId
join c in Customer ON oh.CustomerId equals c.Id into cj
from c in cj.DefaultIfEmpty()
join ia in ItemAvailability on od.ItemId equals ia.RecId into iaj
from ia in iaj.DefaultIfEmpty()
join ivt in Inventory on ia.ItemId equals ivt.ItemId
where oh.DeliveryDate == deliveryDate && oh.OrderType == "Web"
group od by new { ia.ItemId, ivt.ItemName, c.DlvMode, c.DeliveryMethod } into g
select new
{
g.Key.ItemId,
g.Key.DeliveryMethod,
Qty = g.Sum(x => x.Quantity)
} into s
orderby s.ItemId
select s;
SELECT ra.ResidentID, ra.RoomID, r.Number, ra.StartDate, p.FacilityID
FROM(
SELECT ResidentID, MAX(StartDate) AS max_start
FROM RoomAssignments
GROUP BY ResidentID
) m
INNER JOIN RoomAssignments ra
ON ra.ResidentID = m.ResidentID
AND ra.StartDate = m.max_start
INNER JOIN Rooms r
ON r.ID = ra.RoomID
INNER JOIN Person p
ON p.ID = ra.ResidentID
inner join ComplianceStage cs
ON cs.Id = p.ComplianceStageID
ORDER BY ra.EndDate DESC
I'm trying to figure out how to convert this to C# using LINQ. I'm brand new with C# and LINQ and can't get my subquery to fire correctly. Any chance one of you wizards can turn the lights on for me?
Update-----------------
I think I've got the jist of it, but am having trouble querying for the max startdate:
var maxQuery =
from mra in RoomAssignments
group mra by mra.ResidentID
select new { mra.ResidentID, mra.StartDate.Max() };
from ra in RoomAssignments
join r in Rooms on ra.RoomID equals r.ID
join p in Persons on ra.ResidentID equals p.ID
where ra.ResidentID == maxQuery.ResidentID
where ra.StartDate == maxQuery.StartDate
orderby ra.ResidentID, ra.StartDate descending
select new {ra.ResidentID, ra.RoomID, r.Number, ra.StartDate, p.FacilityID}
Following my LINQ to SQL Recipe, the conversion is pretty straight forward if you just follow the SQL. The only tricky part is joining the range variable from the subquery for max start date to a new anonymous object from RoomAssignments that matches the field names.
var maxQuery = from mra in RoomAssignments
group mra by mra.ResidentID into mrag
select new { ResidentID = mrag.Key, MaxStart = mrag.Max(mra => mra.StartDate) };
var ans = from m in maxQuery
join ra in RoomAssignments on m equals new { ra.ResidentID, MaxStart = ra.StartDate }
join r in Rooms on ra.RoomID equals r.ID
join p in Persons on ra.ResidentID equals p.ID
join cs in ComplianceStage on p.ComplianceStageID equals cs.Id
orderby ra.EndDate descending
select new {
ra.ResidentID,
ra.RoomID,
r.Number,
ra.StartDate,
p.FacilityID
};
I have this code
SELECT Sales.InvoiceNo, Sales.SaleDate, Clients.Name, Clients.Address AS
ClientAdrs, Stock.Itemname, Stock.Tax, Stock.Price, Transactions.Qty,
Transactions.NetValue,Transactions.TaxAmount, Transactions.TotalAmount,
Sales.GrossNet, Sales.GrossTax, Sales.GrossTotal, Sales.PrintingCharge,
Sales.LabourCharge, Sales.AdjustableAmount,Sales.GrandTotal, Sales.InWords,
ShopeDetails.Address, ShopeDetails.Email, ShopeDetails.Mobile1,
ShopeDetails.Mobile2, ShopeDetails.TIN, ShopeDetails.AcN,ShopeDetails.IFC
FROM Sales
INNER JOIN Clients
ON Sales.Cid = Clients.Cid
INNER JOIN Transactions
ON Sales.InvoiceNo = Transactions.InvoiceNo
INNER JOIN Stock
ON Transactions.Sid = Stock.Sid
INNER JOIN ShopeDetails
ON Sales.Id = ShopeDetails.Id
and i get this error
Syntax error (missing operator) in query expression
Sales.Cid = Clients.Cid INNER JOIN Transactions ON Sales.InvoiceNo = Transactions.InvoiceNo INNER JOIN Stock ON Transactions.Sid = Stock.Sid INNER JOIN ShopeDetails ON Sales.Id = ShopeDetails.I
Please Help anyone !!
SELECT Sales.InvoiceNo, Sales.SaleDate, Clients.Name, Clients.Address AS
ClientAdrs,Stock.Itemname, Stock.Tax, Stock.Price, Transactions.Qty,
Transactions.NetValue,Transactions.TaxAmount, Transactions.TotalAmount,
Sales.GrossNet, Sales.GrossTax, Sales.GrossTotal, Sales.PrintingCharge,
Sales.LabourCharge, Sales.AdjustableAmount,Sales.GrandTotal, Sales.InWords,
ShopeDetails.Address, ShopeDetails.Email, ShopeDetails.Mobile1,
ShopeDetails.Mobile2, ShopeDetails.TIN, ShopeDetails.AcN,ShopeDetails.IFC
FROM (((Sales INNER JOIN Clients ON Sales.Cid = Clients.Cid)
INNER JOIN Transactions ON Sales.InvoiceNo = Transactions.InvoiceNo)
INNER JOIN Stock ON Transactions.Sid = Stock.Sid )
INNER JOIN ShopeDetails ON Sales.Id = ShopeDetails.Id
this solved the problem
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,
};
I have a query which have multiple conditions on on clause
SELECT *
FROM
CATALOGITEM with (nolock) INNER JOIN CATALOG with (nolock) ON
CATALOGITEM.catalog_id = CATALOG.catalog_id and not(catalog.catalog_id = 21) AND NOT(catalog.catalog_id = 20)
INNER JOIN PRODUCT with (nolock) ON
CATALOGITEM.s_num = PRODUCT .s_num
LEFT OUTER JOIN PRODUCT_DETAIL with (nolock) ON
PRODUCT_DETAIL.s_num = PRODUCT.s_num
WHERE
(
CATALOGITEM.publish_code = 'upd' OR
CATALOG_ITEM.publish_code = 'ins' OR
PRODUCT.publish_code = 'upd' OR
PRODUCT.publish_code = 'ins'
)
and
(CATALOG.unit_id = bu.unit_id)
How to write this in LINQ.
Please advice.
Assume you're missing a join to PRODUCT table?
Anyway, this should get you started.
var query = (from ci in db.catalogitem
join c in db.catalog on ci.catalog_id equals c.catalog_id
join p in db.products on ci.s_num equals p.s_num
join pd in db.productdetail on p.s_num equals pd.s_num into tempprods
from prods in tempprods.DefaultIfEmpty()
where !(c.catalog_id.Contains(21, 20))
&& (ci.publish_code.Contains('upd','ins')) ||
(p.publish_code.Contains('upd','ins'))
select ci)
If you want to preserve the (NOLOCK) hints, I have blogged a handy solution using extension methods in C#. Note that this is the same as adding nolock hints to every table in the query.