How do I write this in linq:
SELECT
c.customerNumber,
IsNull(l.loanNumber, '') AS loanNumber,
dd.documentTypeName,
dd.documentSubTypeName,
d.filename,
(SELECT propertyId FROM SystemProperties WHERE propertyKey LIKE 'acculoan.inputType.Other') AS inputType
FROM
document AS d INNER JOIN qryDocumentDefinitions AS dd
ON d.documentDefId = dd.documentDefId
AND d.documentId = #DocumentId
INNER JOIN customer AS c
ON c.customerId = d.customerId
LEFT OUTER JOIN loan AS l
ON l.loanId=d.loanId
Help needed with the isnull especially and the nested select.
Many thanks in advance..!
You can try the below (have not been able to test, maybe will require some adjustments, but should be in the same form);
from Doc in db.document
join DocDef in db.qryDocumentDefinitions on doc.documentDefId equals DocDef.documentDefId
join Cus in db.Customer on Cus.customerId = Doc.customerId
from loan in db.loan
.Where(loan => loan.loanId == Doc.loanId).DefaultIfEmpty()
select new
{
CustNo= Cus.customerNumber
,loan == null ? "" : loan.loanNumber
,DocType= DocDef.documentTypeName
,DocSubType= DocDef.documentSubTypeName
,filename = Doc.filename
,inputType = (?int)(from Sys in db.SystemProperties
where Sys.propertyKey.Contains("acculoan.inputType.Other")
}
Related
I am having trouble turning the following sql code into LINQ. I have it mostly working but I can't figure out how to get the revision from the items table which is gotten in those subqueries. Any help would greatly be appreciated.
Here is the SQL query:
SELECT
dbo.SerialOptCon.GROUPID, TopRevsion.RevName AS NAME,
dbo.SerailOptionNo.PartNo, dbo.SerialOptCon.PRICE,
0.00 AS DISCOUNT,
dbo.GroupConfig.CURRENCY,
'OPTION' AS TYPE, dbo.SerialModelGroupOptions.MODEL,
dbo.SerialModelGroupOptions.SEQUENCE
FROM
dbo.SerialModelGroupOptions
INNER JOIN
dbo.SerialOptCon
INNER JOIN
dbo.SerailOptionNo ON dbo.SerialOptCon.OptionId = dbo.SerailOptionNo.ProductOptionId
ON dbo.SerialModelGroupOptions.OptionPartNo = dbo.SerailOptionNo.PartNo
LEFT OUTER JOIN
dbo.GroupConfig ON dbo.SerialOptCon.GROUPID = dbo.GroupConfig.ID
LEFT OUTER JOIN
(SELECT itm1.PartNo, itm1.Revision, itm1.RevName
FROM dbo.Items AS itm1
INNER JOIN
(SELECT PartNo, MAX(Revision) AS Revision
FROM dbo.Items
GROUP BY PartNo) AS itm2 ON itm1.PartNo = itm2.PartNo
AND itm1.Revision = itm2.Revision) AS TopRevsion ON dbo.SerailOptionNo.PartNo = TopRevsion.PartNo
WHERE
(dbo.SerialOptCon.GROUPID = '01d2b4d2-ad63-4eab-a83f-f31ea0f6274b')
AND (dbo.SerialModelGroupOptions.MODEL = 'abcde')
ORDER BY
SEQUENCE
Here is the LINQ query that I have currently:
foreach (var item in priceGroupDetailsViewModels)
{
(from pmo in _context.SerialModelGroupOptionss
join po in _context.SerailOptionNos on pmo.OptionPartNo equals po.PartNo
join pop in _context.dbo.SerialOptCon on po.SerailOptionNoId equals pop.SerailOptionNoId
join con in _context.GroupConfig on pop.GroupId equals con.GroupId
into p
from pmp in p.DefaultIfEmpty()
join it in _context.Items on po.PartNo equals it.PartNo
where pmp.PriceGroupId == item.PriceGroupId && pmo.Model == item.Name
select new OptionsDetails
{
GroupId = pop.GroupId,
PartNo = po.PartNo,
Currency = pmp.Currency,
Sequence = pmo.Sequence,
Description = it.Description3
});
}
Hi we have a legacy code which we are trying to modify and trying to get rid of "for XML". the current code is as below
Select
a.columnidfk as columnid,
tvs.columnversion as columnversion,
a.newcolumnId as newcolumnid,
(Select
at.columnTrackingId as columntrackingid,
at.newcolumnIdFk as newcolumnid,
at.IDBSIdFk as idbid,
i.Name as iname,
IsNull(convert(varchar,at.colidbdate,101),'') as colidbdate,
IsNull(convert(varchar,at.colidbdate2,101),'') as colidbdate2,
IsNull(at.colReason, '') as colReason,
IsNull(d.DispositionText, '') as DispositionText,
at.status as status
From columnTracking at
Left Outer Join LkIDBs i on at.IDBSIdFk = i.IDBSId
left outer join LkDisposition d On at.[colDecisionIdFk] = d.DispositionId
Where at.status in('Active','Inactive') and at.newcolumnIdFk = a.newcolumnId and (i.status in ('Active','Inactive') OR i.status is null) and (d.status='Active' or d.status is null)
for Xml path('columntracking'), type)
From tablecolumns a
Left Outer Join columnVersions tvs on a.columnversionidfk = tvs.columnversionid Where
a.columnVersionIdFk in
(Select columnVersionId From columnVersions tv
Inner Join tabcolumns t On tv.colIdFk = t.colId
Where t.TicketNo = #TicketNo )
order by a.TicketVersionIdFk , a.assessmentid
For Xml path('columntab'), Root('columns')
when i remove the "for Xml path('columntracking'), type" and "For Xml path('columntab'),Root('columns')" im getting the below error
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
Please advise !
I believe i got the desired table out by Joining the (first xml out put)
Select
a.columnidfk as columnid,
tvs.columnversion as columnversion,
a.newcolumnId as newcolumnid,
att.columntrackingid,
att.newcolumnid,
att.idbid,
att.iname
From tablecolumns a
Left Outer Join columnVersions tvs on a.columnversionidfk = tvs.columnversionid Where
left outer join (
Select
at.columnTrackingId as columntrackingid,
at.newcolumnIdFk as newcolumnid,
at.IDBSIdFk as idbid,
i.Name as iname,
IsNull(convert(varchar,at.colidbdate,101),'') as colidbdate,
IsNull(convert(varchar,at.colidbdate2,101),'') as colidbdate2,
IsNull(at.colReason, '') as colReason,
IsNull(d.DispositionText, '') as DispositionText,
at.status as status
From columnTracking at
Left Outer Join LkIDBs i on at.IDBSIdFk = i.IDBSId
left outer join LkDisposition d On at.[colDecisionIdFk] = d.DispositionId
Where at.status in('Active','Inactive') and (i.status in ('Active','Inactive') OR i.status is null) and (d.status='Active' or d.status is null)
) att on att.newcolumnid = a.newcolumnId
and a.columnVersionIdFk in
(Select columnVersionId From columnVersions tv
Inner Join tabcolumns t On tv.colIdFk = t.colId
Where t.TicketNo = #TicketNo )
In a database with several tables and relationships between them, I want to select only the most recent payment by the clients last year.
My query goes like this:
SELECT
P.Name,
EV.EventName,
FN.Installments,
FN.PurchaseValue,
FN.DueDate
FROM ClientPrivate PF
JOIN Client P ON P.PesControle = PF.PesControle
JOIN ClientClass CP ON P.PesControle = CP.PesControle
JOIN EVENT EV ON CP.EveControle = EV.EveControle
JOIN Class cc ON cc.CurControle = EV.CurControle
JOIN Finance FN ON FN.PesControle = P.PesControle
It returns the values I need, only I'd like to get only the most recent purchase by each client, instead of all of them.
I edited to help clarify. The 'Controle' columns are the keys.
Whatever you date column is put that in the ORDER BY Clause of the ROW_NUMBER() function and you are good to go.
;WITH CTE AS
(
SELECT
P.PesNome,
EV.EveDescri,
FN.FinTotParc,
FN.FinVlrLiquido,
FN.FinDiaVencto,
ROW_NUMBER() OVER (PARTITION BY P.PesNome ORDER BY [DateColumn] DESC) rn
FROM PessoaFisica PF
JOIN Pessoa P ON P.PesControle = PF.PesControle
JOIN CursoPessoa CP ON P.PesControle = CP.PesControle
JOIN EVENTO EV ON CP.EveControle = EV.EveControle
JOIN Curso cc ON cc.CurControle = EV.CurControle
JOIN Financeiro FN ON FN.PesControle = P.PesControle
)
SELECT *
FROM CTE
WHERE rn = 1
I'm guessing Financeiro.FinDiaVencto is your purchase date, and that Pessoa.PesControle is a unique identifier for a person. If not, you'll need to modify the query. I'm using Google to translate Portuguese, and it's still not clear to me.
SELECT
P.PesNome,
EV.EveDescri,
FN.FinTotParc,
FN.FinVlrLiquido,
FN.FinDiaVencto
FROM PessoaFisica PF
JOIN Pessoa P
ON P.PesControle = PF.PesControle
JOIN CursoPessoa CP
ON P.PesControle = CP.PesControle
JOIN EVENTO EV
ON CP.EveControle = EV.EveControle
JOIN Curso cc
ON cc.CurControle = EV.CurControle
JOIN Financeiro FN
on FN.PesControle = P.PesControle
WHERE EXISTS (
SELECT 1
FROM PessoaFisica PF_s
JOIN Pessoa P_s
ON P_s.PesControle = PF_s.PesControle
JOIN CursoPessoa CP_s
ON P_s.PesControle = CP_s.PesControle
JOIN EVENTO EV_s
ON CP_s.EveControle = EV_s.EveControle
JOIN Curso cc_s
ON cc_s.CurControle = EV_s.CurControle
JOIN Financeiro FN_s
on FN_s.PesControle = P_s.PesControle
WHERE P_s.PesControle = P.PesControle
HAVING MAX(FN_s.FinDiaVencto) = FN.FinDiaVencto
)
That should return the record with the most recent Financeiro.FinDiaVencto for each Pessoa.PesControle. If you need it by course or by event(?) you'll need to modify the WHERE clause to join on those fields, too.
I'm not sure if that join to PessoaFisica is necessary either, but I included it because it could be eliminating records. You can include a GROUP BY in the subquery, but it should be redundant.
I need help to convert this query from LINQ syntax into SQLServer query syntax:
var t1 = (from p in db.Varors join
op in db.OrderVarors on p.id equals op.IdVara
where op.IdOrder == OrderId
select p).ToList();
Hope this helps:-
SELECT v.* from Varors v
JOIN ordervarors ov on v.id = ov.id
where ov.idOrder == #OrderId
You may want to choose individual columns instead of star but this should work.
select *
from dbo.Varors as P
join dbo.OrderVarors as OP
on p.id equals op.IdVara
where op.IdOrder = OrderId
this is a simple join - try this :
select Varors.*
from Varors
join OrderVarors on Varors.id = OrderVarors.IdVara
where OrderVarors.IdOrder = #OrderId
I have ran into a snag with my Linq-to-Sql.
I have a sql query that runs the way I want and usually I use Linqer to convert to Linq to see the general idea. But this time my SQL query seems to advanced for Linqer. :/
I think the problem is the INNER JOINS that are nested in the LEFT OUTER JOIN. Unfortunately I have never ran into this before and don't know how to solve it using Linq.
My SQL query looks like this:
SELECT c.[Company], c.[Name_First], c.[Name_Last], ort.[IDOriginatorRoleType],
ort.[RoleType] AS [OriginatorRoleType], o.[IDOriginator], o.[IDWork],
o.[IDContact], m.[IDMedia], m.[IDWork], m.[FileName], m.[FileNameOnDisk],
m.[DateAdded], w.[IDWork] AS [IDWork2], w.[ArticleNumber], w.[Title],
w.[FrontPageLow], w.[FrontPageLowOnDisk], w.[FrontPageHigh],
w.[FrontPageHighOnDisk]
FROM [dbo].[tblSubscriptionsWorks] AS sw
INNER JOIN [dbo].[tblWorks] AS w ON sw.[IDWork] = w.[IDWork]
LEFT OUTER JOIN [dbo].[tblMedias] AS m ON m.[IDWork] = w.[IDWork]
LEFT OUTER JOIN ([dbo].[tblOriginators] AS o
INNER JOIN [dbo].[tblOriginatorRoles] AS ors ON
o.[IDOriginatorRole] = ors.[IDOriginatorRole]
INNER JOIN [dbo].[tblOriginatorRoleTypes] AS ort ON
ors.[IDOriginatorRoleType] = ort.[IDOriginatorRoleType]
INNER JOIN [dbo].[tblContacts] AS c ON
o.[IDContact] = c.[IDContact]) ON
(o.[IDWork] = w.[IDWork]) AND (ort.[IDOriginatorRoleType] = 1)
WHERE sw.[IDWork_Subscription] = 9942
The left outer join is not a problem what I can see. You just have to divide the statement
LEFT OUTER JOIN ([dbo].[tblOriginators] AS o
INNER JOIN [dbo].[tblOriginatorRoles] AS ors ON
o.[IDOriginatorRole] = ors.[IDOriginatorRole]
INNER JOIN [dbo].[tblOriginatorRoleTypes] AS ort ON
ors.[IDOriginatorRoleType] = ort.[IDOriginatorRoleType]
INNER JOIN [dbo].[tblContacts] AS c ON
o.[IDContact] = c.[IDContact]) ON
(o.[IDWork] = w.[IDWork]) AND (ort.[IDOriginatorRoleType] = 1)
into another IQueryable list. In the example the variable db is the datacontext. Here is a suggestion to a solution:
//selects all the columns that is just in the select from the left join
var leftJoin=
(
from o in db.tblOriginators
join ors in db.tblOriginatorRoles
on o.IDOriginatorRole equals ors.IDOriginatorRole
join ort in db.tblOriginatorRoleTypes
on ors.IDOriginatorRoleType equals ort.IDOriginatorRoleType
join c in db.tblContacts
on o.IDContact equals c.IDContact
where ort.IDOriginatorRoleType==1
select new
{
o.IDWork,
c.Company,
c.Name_First,
c.Name_Last,
ort.IDOriginatorRoleType,
ort.RoleType,
o.IDOriginator,
o.IDContact
}
);
var output=(
from sw in db.tblSubscriptionsWorks
join w in db.tblWorks
on sw.IDWork equals w.IDWork
from m in db.tblMedias
.Where(x=>x.IDWork==w.IDWork).DefaultIfEmpty()
//Left join with the IQueryable list
from org in leftJoin
.Where(x =>x.IDWork==w.IDWork).DefaultIfEmpty()
where
sw.IDWork_Subscription == 9942
select new
{
org.Company,
org.Name_First,
org.Name_Last,
org.IDOriginatorRoleType,
OriginatorRoleType=org.RoleType,
org.IDOriginator,
org.IDWork,
m.IDMedia,
m.IDWork,
m.FileName,
m.FileNameOnDisk,
w.FrontPageLow,
w.FrontPageLowOnDisk,
w.FrontPageHigh,
w.FrontPageHighOnDisk
}
);