How could i improve this Linq code instead of more joins? - entity-framework

I have this Linq code as below:
from objpath in Path
join objOriginalStation in Station on objpath.OriginalStationID equals objOriginalStation.StationID
join objDestinationStation in Station on objpath.DestinationStationID equals
objDestinationStation.StationID
join objVia1Station in Station on objpath.Via1StationID equals objVia1Station.StationID
join objVia2Station in Station on objpath.Via2StationID equals objVia2Station.StationID
select new ViewModels.PathViewModel
{
PathID = objpath.PathID,
SubnetworkID = objpath.SubnetworkID,
Distance = objpath.Distance,
DestinationStationID = objpath.DestinationStationID,
OriginalStationID = objpath.OriginalStationID,
Via1StationID = objpath.Via1StationID,
Via2StationID = objpath.Via2StationID,
Version = objpath.Version,
OriginalStationName = objOriginalStation.StationName,
DestinationStationName = objDestinationStation.StationName,
Via1StationName = objVia1Station.StationName,
Via2StationName = objVia2Station.StationName,
};
but I thought it's not perfect and I want to improve it, anybody has suggestion?

Related

How to reduce x select to 1 select?

I have the following 3 selects and I want to improve performance, what can I do?
select object into lastUpdate
from data
where predicate = '#lastUpdate'
and subject = 'subject1';
select object into latitude
from data
where predicate = '#latitude'
and subject = 'subject1';
select object into longitude
from data
where predicate = '#longitude'
and subject = 'subject1';
Thank you
You can use a JOIN:
select lu.object, lat.object, lon.object
into lastupdate, latitude, longitude
from data lu
left join data lat on lat.subject = lu.subject and lat.predicate = '#latitude'
left join data lon on lon.subject = lu.subject and lon.predicate = '#longitude'
where lu.subject = 'subject1'
and lu.predicate = '#lastUpdate';

Incorrect Syntax multi part identifier

Currently I'm getting an incorrect syntax but can't locate its origins. Any thoughts on what i'm missing. Multi-part identifier pvxme.mt couldn't be found.
SELECT
PVXME.MT
PVXME.MT_VERSION
PVXME.START_DATE_LOCAL
PVXMEDS.CREATION_DATE_LOCAL
PVXMIHS.USER_NAME
PVXMEDE.PAT_ID
PVXMEDE.STRING_VALUE
PVXMEDE.NUM_VALUE AS MEDE_NUM_VALUE
RVXMIIF2.II
RVXMIIF2.NUM_VALUE AS MIIF_NUM_VALUE;
FROM
PVXMEDE
RIGHT OUTER JOIN
PVXMEDG ON PVXMEDG.ME = PVXMEDE.ME;
RIGHT OUTER JOIN
PVXMEDG ON PVXMEDG.SEQUENCE = PVXMEDE.SEQUENCE
AND PVXMEDG.SOURCE_TP = PVXMEDE.SOURCE_TP
AND PVXMEDG.SOURCE_ID = PVXMEDE.SOURCE_ID
AND PVXMEDG.SOURCE_VERSION = PVXMEDE.SOURCE_VERSION;
RIGHT OUTER JOIN
PVXMEDS ON PVXMEDS.ME = PVXMEDG.ME
AND PVXMEDS.SEQUENCE=PVXMEDG.SEQUENCE;
RIGHT OUTER JOIN
PVXME ON PVXME.ME = PVXMEDS.ME AND PVXME.MT=;
RIGHT OUTER JOIN
PVXMI ON PVXMI.MI = PVXME.MI;
LEFT OUTER JOIN
PVXMIHS ON PVXMIHS.MI = PVXME.MI;
LEFT OUTER JOIN
PVXMIID ON PVXME.ME = PVXMIID2.ME;
LEFT OUTER JOIN
RVXMIII ON PVXMIID2.MI = RVXMIIF2.MI
AND PVXMIID2.ID = RVXMIIF2.ID
AND PVXMIID2.ID_SEQUENCE = RVXMIIF2.ID_SEQUENCE
AND PVXMIID2.ME = RVXMIIF2.ME;
WHERE
(PVXMIHS.USER_NAME <> 'sipat'
AND PVXMIHS.WHAT = 'MethodPrepare'
AND PVXME.CX_STRING_4 = '20190117-7h40m'
AND PVXME.MT LIKE 'MK-0431%Tab CA%'
AND PVXMEDE.PAT_ID NOT LIKE '%Spectrum')
AND (RVXMIIF2.II = 'LeverageLimit'OR RVXMIIF2.II = 'XresidualLimit')
Try this:
do put a colon after each column in the SELECT list of columns
do NOT put a semicolon at the end of the list of columns and of each JOIN condition
do complete the RIGHT OUTER JOIN that references PVXME.MT = .......
Code:
SELECT
PVXME.MT,
PVXME.MT_VERSION,
PVXME.START_DATE_LOCAL,
PVXMEDS.CREATION_DATE_LOCAL,
PVXMIHS.USER_NAME,
PVXMEDE.PAT_ID,
PVXMEDE.STRING_VALUE,
PVXMEDE.NUM_VALUE AS MEDE_NUM_VALUE,
RVXMIIF2.II,
RVXMIIF2.NUM_VALUE AS MIIF_NUM_VALUE
FROM
PVXMEDE
RIGHT OUTER JOIN
PVXMEDG P1 ON P1.ME = PVXMEDE.ME -- use table alias P1 here
RIGHT OUTER JOIN
PVXMEDG P2 ON P2.SEQUENCE = PVXMEDE.SEQUENCE -- use table alias P2 here
AND P2.SOURCE_TP = PVXMEDE.SOURCE_TP
AND P2.SOURCE_ID = PVXMEDE.SOURCE_ID
AND P2.SOURCE_VERSION = PVXMEDE.SOURCE_VERSION
RIGHT OUTER JOIN
PVXMEDS ON PVXMEDS.ME = P2.ME
AND PVXMEDS.SEQUENCE = P2.SEQUENCE
RIGHT OUTER JOIN
PVXME ON PVXME.ME = PVXMEDS.ME AND PVXME.MT = ?????? -- ** COMPLETE THIS! **
RIGHT OUTER JOIN
PVXMI ON PVXMI.MI = PVXME.MI
LEFT OUTER JOIN
PVXMIHS ON PVXMIHS.MI = PVXME.MI
LEFT OUTER JOIN
PVXMIID ON PVXME.ME = PVXMIID2.ME
LEFT OUTER JOIN
RVXMIII ON PVXMIID2.MI = RVXMIIF2.MI
AND PVXMIID2.ID = RVXMIIF2.ID
AND PVXMIID2.ID_SEQUENCE = RVXMIIF2.ID_SEQUENCE
AND PVXMIID2.ME = RVXMIIF2.ME
WHERE
(PVXMIHS.USER_NAME <> 'sipat'
AND PVXMIHS.WHAT = 'MethodPrepare'
AND PVXME.CX_STRING_4 = '20190117-7h40m'
AND PVXME.MT LIKE 'MK-0431%Tab CA%'
AND PVXMEDE.PAT_ID NOT LIKE '%Spectrum')
AND (RVXMIIF2.II = 'LeverageLimit'OR RVXMIIF2.II = 'XresidualLimit')

Implementing Concat + RANK OVER SQL Clause in C# LINQ

I need to implement the following T-SQL clause ....
SELECT
CONCAT( RANK() OVER (ORDER BY [Order].codOrder, [PackedOrder].codPackedProduct ), '/2') as Item,
[Order].codOrder as [OF],
[PackedOrder].codLine as [Ligne],
[PackedOrder].codPackedProduct as [Material], ----------------------
[Product].lblPProduct as [Product],
[PackedProduct].lblPackedProduct as [MaterialDescription],
[PackedOrder].codPackedBatch as [Lot],
[Product].codCustomerColor as [ReferenceClient],
[PackedOrder].nbrPackedQuantity as [Quantity],
[PackedOrder].nbrLabelToPrint as [DejaImprime]
FROM [Order] INNER JOIN PackedOrder
ON [Order].codOrder = PackedOrder.codOrder INNER JOIN Product
ON [Order].codProduct = Product.codProduct INNER JOIN PackedProduct
ON PackedOrder.codPackedProduct = PackedProduct.codPackedProduct
Where [Order].codOrder = 708243075
So Far, I'm able to do:
var result =
from order1 in Orders
join packedorder1 in PackedOrders on order1.codOrder equals packedorder1.codOrder
join product1 in Products on order1.codProduct equals product1.codProduct
join packedproduct1 in PackedProducts on packedorder1.codPackedProduct equals packedproduct1.codPackedProduct
where order1.codOrder == _order.codOrder
select new FinishedProductPrintingM
{
OF = order1.codOrder,
Ligne = packedorder1.codLine,
Material = packedorder1.codPackedProduct,
Produit = product1.codProductType,
MaterialDescription = packedproduct1.lblPackedProduct,
Lot = packedorder1.codPackedBatch,
RéférenceClient = product1.codCustomerColor,
Quantité = packedorder1.nbrPackedQuantity,
Déjàimprimé = packedorder1.nbrLabelPrinted
};
Please let me know if its possible or not. I need to display the Items in such a way.Please feel free to add your valuable comments.
I am not aware how to use concat and Rank over function in LINQ.
Can anyone help me to convert my SQL query into LINQ?

Can I JOIN table on the basis of the case statement in PostgreSQL

Can I JOIN the table on the basis of the case statement in PostgreSQL. I have written the one SQL in stored procedure into that I'm passing the one flag on that basis I want to jon the table. Please see the case statement,
JOIN lease_intervals li_active
ON ( li_active.cid = l.cid AND l.id = li_active.lease_id AND
l.active_lease_interval_id = li_active.id )
LEFT JOIN applications a
ON ( a.cid = li.cid AND li.lease_id = a.lease_id AND
a.lease_interval_id = li.id )
**CASE
WHEN pIsFromUI = TRUE
THEN JOIN property_integration_databases pid ON ( pid.cid = l.cid AND pid.property_id == l.property_id )
JOIN integration_databases id ON ( id.id = pid.integration_database_id AND id.cid = pid.cid )
ELSE
1
END**
Please let me know is there any alternate solution for above.
join
lease_intervals li_active on
li_active.cid = l.cid and l.id = li_active.lease_id and
l.active_lease_interval_id = li_active.id
left join
applications a on
a.cid = li.cid and li.lease_id = a.lease_id and
a.lease_interval_id = li.id
left join
property_integration_databases pid on
pid.cid = l.cid and pid.property_id = l.property_id and pisfromui
left join
integration_databases id on
id.id = pid.integration_database_id and id.cid = pid.cid and pisfromui

Can some one help me to conver it into LINQ i m using Entity Framework

select ForumCategories.ID , ForumCategories.Title , ForumCategories.DateCreated,
CO = ( select COUNT(*) from ForumSubCategories where ForumSubCategories.CategoryID_FK = ForumCategories.ID)
from ForumCategories
var q = from fc in Context.ForumCategories
select new
{
Id = fc.ID,
Title = fc.Title,
DateCreated = fc.DateCreated
CO = fc.ForumSubCategories.Count()
};
return q;
The "join" (subquery) is implicit; it's defined in the relationship between ForumCategories and ForumSubCategories in your model. Using this syntax, the call to Count() will be done on the DB server.