I'm developing an ASP.NET website this website is connecting through a dataset to database at the starting of the application I fill the database with the right information then I need to make a request to show the data on the app.
For that I'm trying to create a view with the join of many tables. I've already tried this view on SQL Server but now I want to do the same with Linq on my program, but one of my join need two conditions.
1st one: the join between the two table
2nd one: A condition to select the right index
SQL:
dbo.JOBPART AS jp1
LEFT OUTER JOINdbo.JOBPARAMETER AS p0 ON jp1.JOB_PART_ID = p0.JOB_PART_ID AND p0.PARAM_INDEX = 0
LEFT OUTER JOIN dbo.JOBPARAMETER AS p1 ON jp1.JOB_PART_ID = p1.JOB_PART_ID AND p1.PARAM_INDEX = 1
var view_JobPart = from jp1 in partTable
join p0 in PrmTable on jp1[JOB_PART_FIELD_ID] equals p0[JOB_PARAMETER_FIELD_PART_ID]
join p1 in PrmTable on jp1[JOB_PART_FIELD_ID] equals p1[JOB_PARAMETER_FIELD_PART_ID]
select new
{
jp1.JOB_PART_ID,
jp1.JOB_MAIN_ID,
jp1.PREV_JOB_PART_ID,
NEXT_JOB_ID = jp2.JOB_PART_ID,
jp1.JOB_ACTION_ID,
ja.JOB_ACTION_NAME,
};
// the parameter index is missing
How can I add this second condition?
Related
I am trying to get all data from the join table to data review, but what I learn is just to specify the attributes one by one in the SELECT statement as following code:
var query = (from inv in db.tblinventories
join sup in db.tblsuppliers on inv.SupplierID equals sup.SupID into left
from sup in left.DefaultIfEmpty()
select new { inv.Invnum,inv.Model, sup.name }).ToList();
this.dgvPOS.DataSource = query;```
Is this the only way to fetch all data? Because in ORACLE SQL the way is quite simple by using "*" to represent all attributes like this:
SELECT *
FROM tblinventories
LEFT JOIN tblsuppliers
ON tblinventories.SupplierID = tblsuppliers.SupID;
I have an linq query like this :
var query = from Romm in RoMM
join rfrsa in RoMmfrsa on Romm.RoMmid equals rfrsa.RoMmid
join frsa in Frsa on rfrsa.Frsaid equals frsa.Fraid
join fra in Fra on frsa.Fraid equals fra.Fraid
where Romm.ActTypeId == 2 && Romm.SegmentId == 4
select new
{
Romm.ActTypeId,
Romm.RoMmid,
frsa.Fraid,
frsa.Frsaid,
Romm.ImpactId
};
And I have SQL code as below :
SELECT romm.ROMMID
, frsa.FRAID
, frsa.FRSAID
, romm.ImpactID
FROM RoMM AS romm
INNER
JOIN RoMMFRSA AS rfrsa
ON romm.RoMMID = rfrsa.RoMMID
INNER
JOIN FRSA AS frsa
ON rfrsa.frsaid = frsa.frsaid
INNER
JOIN FRA AS fra
ON frsa.FRAID = fra.FRAID
WHERE romm.acttypeid = 2
AND romm.segmentid = 4
The SQL only shows one row (which is correct), the linq shows the correct row and then it displays about another 3 rows which is not what we need. I need the linq to show one row which is correct with the SQL. Is this because of maybe many-many relationships ?
Looks like a typo in either the C# or the SQL join:
SQL: ON rfrsa.frsaid = frsa.frsaid
C#: rfrsa.Frsaid equals frsa.Fraid
^^^^^^
mismatch here
This question already has answers here:
How to do joins in LINQ on multiple fields in single join
(13 answers)
Closed 1 year ago.
I am struggling with this left outer join with multiple conditions in LINQ. My initial SQL statement is as follows.
SELECT DISTINCT [Product].[prod_id],
[Product].[brd_id],
[Product].[prod_brdId],
[Size].[size_id],
[Size].[size_sort],
[Bin].[bin_rack],
[Bin].[bin_alpha]
FROM [Proddetails]
JOIN [Prodcolor]
ON [Proddetails].[pcl_id] = [Prodcolor].[pcl_id]
JOIN [Product]
ON [Prodcolor].[prod_id] = [Product].[prod_id]
JOIN [Size]
ON [Proddetails].[pdt_size] = [Size].[size_id]
LEFT OUTER JOIN [Bin]
ON [Product].[prod_id] = [Bin].[prod_id]
ORDER BY [Product].[brd_id], [Product].[prod_brdId], [Size].[size_sort]
And my corresponding LINQ statement in .NET
BinProds = (
from pd in applicationDbContext.Proddetails
join pc in applicationDbContext.Prodcolors on pd.PclId equals pc.PclId
join pr in applicationDbContext.Products on pc.ProdId equals pr.ProdId
join sz in applicationDbContext.Sizes on pd.PdtSize equals sz.SizeId
join bn in applicationDbContext.Bins on pr.ProdId equals bn.ProdId into ps
from bn in ps.DefaultIfEmpty()
select new CoreBin
{
... fields here
}
).Distinct().OrderBy(i=>i.BrdId).ThenBy(j=>j.ProdBrdId).ThenBy(k=>k.SizeSort).ToList();
So, both of these execute successfully in SQL Server Studio and .NET respectively. However it's not giving me the exact result I'm looking for because it's missing one additional condition on the LEFT OUTER JOIN. Below is the one change I made to my SQL statement which gives me exactly what I want in SQL Server Studio. I just can't figure out how to adjust the LINQ to get the same result in my .NET project.
LEFT OUTER JOIN [Bin]
ON [Product].[prod_id] = [Bin].[prod_id]
AND [Bin].[size_id] = [Size].[size_id]
I'm hoping there is a reasonable adjustment. If you need my table outputs or db design, plz let me know.
This one should give you desired SQL:
.....
join bn in applicationDbContext.Bins
on new { pr.ProdId, sz.size_id } equals new { bn.ProdId, bn.size_id } into ps
from bn in ps.DefaultIfEmpty()
select new CoreBin
{
... fields here
}
.....
I have data in two tables and I need in one query get all data and join getting data.
SELECT
kpip.PersonalName,
kpiT.Name,
kpiPR.KpiTarget,
kpiPR.KpiResultDate,
kpiPR.KpiResult
FROM KpiPersonalResult AS kpiPR join KpiPersonal as kpip
on kpiPR.KpiPersonal = kpip.Id join KpiType AS kpiT
on kpip.KpiType = kpiT.Id join MerchantAdministrators as merA
on kpiPR.KpiAdded = merA.Id and kpiPR.KpiResultDate between '2021-04-07' and '2021-04-08'
select
kpiP.PersonalName,
kpiT.Name,
kpiP.KpiTarget
from KpiPersonal as kpiP join KpiType as kpiT
on kpiP.KpiType = kpiT.Id
Based on the fast that the second query has 3 columns of the same name as the first query, I guess you mean to union them:
SELECT
kpip.PersonalName,
kpiT.Name,
kpiPR.KpiTarget,
kpiPR.KpiResultDate,
kpiPR.KpiResult
FROM
KpiPersonalResult AS kpiPR
join KpiPersonal as kpip on kpiPR.KpiPersonal = kpip.Id
join KpiType AS kpiT on kpip.KpiType = kpiT.Id
join MerchantAdministrators as merA on kpiPR.KpiAdded = merA.Id and kpiPR.KpiResultDate between '2021-04-07' and '2021-04-08'
UNION ALL
select
kpiP.PersonalName,
kpiT.Name,
kpiP.KpiTarget,
null, --put suitable default values for the other columns here
null
from
KpiPersonal as kpiP
join KpiType as kpiT on kpiP.KpiType = kpiT.Id
Unioned queries need the same number of columns. I've inserted NULL as default value for the two missing columns in the second query (relative to the first)
UNION makes a resultset grow taller. If you intended for it to grow wider, that is done via JOIN. A simple pattern for doing so is:
WITH query1 AS(
--query 1 here
), query2 AS (
--query2 here
)
SELECT * FROM query1 JOIN query2 ON ...
Side note on formatting and indenting - most people find SQL most readable when all operations that are related are at the same indent level e.g in a typical query, the SELECT FROM WHERE GROUP ORDER keywords are all at the same indent level, with the blocks that relate to them (the list of selected columns, or list of joined tables, list of where'd predicates etc) indented a level again. We also typically don't use as when aliasing tables but we do use it when aliasing columns in the SELECT
i'm having trouble with left outer join
i only want the cj_id(table1 in bd1) where c.ref(table1 in bd1) is not found in ref(table2 in bd2)
so i can remove some registries from db1 that have cj_id instead of ref
for this i'm using this code:
var query1 = from a in dbPT.table2
join b in dataB.table1
on a.Ref equals b.ref into c
from x in c.DefaultIfEmpty()
select x.CJ_ID;
i can't get it to return a string with a cj_id
has you can see in here Joining Two Entity Sets from Different Contexts
the join is done in the server side, and if you use diferent server there is no way you can do this