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
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 the following SQL statement to get data from multiple tables inside my SQL Server database:
SELECT max(ad.ORG_NAME) AS "Account", wo.WORKORDERID AS "Request ID", max(aau.FIRST_NAME) AS "Requester", max(aci.emailid) "Email ID",max(wo.TITLE) AS "Request Title", max(ti.FIRST_NAME) AS "Technician", longtodate(max(srm.Responsetime)) AS "Survey responded Time", max(srcmt.COMMENTTEXT) AS "Comments",max(srm.result) "Over All Satisfaction Level" FROM SurveyResponseRequestMapping srrm
INNER JOIN Survey_Response_Main srm ON srrm.RESPONSEID=srm.RESPONSEID
INNER JOIN Survey_Response_Answer sra ON srm.RESPONSEID=sra.RESPONSEID
LEFT JOIN Survey_Response_Comment srcmt ON srm.RESPONSEID=srcmt.RESPONSEID
LEFT JOIN WorkOrder wo ON srrm.WORKORDERID=wo.WORKORDERID
LEFT JOIN WorkOrderStates wos ON wo.WORKORDERID=wos.WORKORDERID
LEFT JOIN SDUser td ON wos.OWNERID=td.USERID
LEFT JOIN AaaUser ti ON td.USERID=ti.USER_ID
LEFT JOIN SDUser sdu ON wo.REQUESTERID=sdu.USERID
LEFT JOIN AaaUser aau ON sdu.USERID=aau.USER_ID
INNER JOIN AccountSiteMapping asm ON wo.siteid=asm.siteid
INNER JOIN AccountDefinition ad ON asm.accountid=ad.org_id
left join aaausercontactinfo auci on auci.user_id = sdu.userid
left join aaacontactinfo aci on aci.contactinfo_id = auci.contactinfo_id
where srm.RESPONSETIME>=DATETOLONG('2021-03-22 00:00:00') and srm.RESPONSETIME<=DATETOLONG('2021-03-31 23:59:59')
GROUP BY wo.WORKORDERID order by 7
My question is if can I run this raw SQL statement inside my .NET core console application and map the returned columns into C# variables?
Thanks
You can use Dapper for run Raw SQL code and set value to class
Example
var data = await connection.QueryAsync<YourClass>(sql, parameters);
Note: you can create new class then create property like your query must some (Name and Datatype)
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'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?
I want to have following type of query in entity frame work
SELECT c2.*
FROM Category c1 INNER JOIN Category c2
ON c1.CategoryID = c2.ParentCategoryID
WHERE c1.ParentCategoryID is NULL
How to do the above work in Entity framework...
Well, I don't know much about EF, but that looks something like:
var query = from c1 in db.Category
where c1.ParentCategoryId == null
join c2 in db.Category on c1.CategoryId equals c2.ParentCategoryId
select c2;
Just to tidy this up the following is a bit nicer and does the same thing:
var query = from c1 in db.Category
from c2 in db.Category
where c1.ParentCategoryId == null
where c1.CategoryId == c2.ParentCategoryId
select c2;
In EF 4.0+, LEFT JOIN syntax is a little different and presents a crazy quirk:
var query = from c1 in db.Category
join c2 in db.Category on c1.CategoryID equals c2.ParentCategoryID
into ChildCategory
from cc in ChildCategory.DefaultIfEmpty()
select new CategoryObject
{
CategoryID = c1.CategoryID,
ChildName = cc.CategoryName
}
If you capture the execution of this query in SQL Server Profiler, you will see that it does indeed perform a LEFT OUTER JOIN. HOWEVER, if you have multiple LEFT JOIN ("Group Join") clauses in your Linq-to-Entity query, I have found that the self-join clause MAY actually execute as in INNER JOIN - EVEN IF THE ABOVE SYNTAX IS USED!
The resolution to that? As crazy and, according to MS, wrong as it sounds, I resolved this by changing the order of the join clauses. If the self-referencing LEFT JOIN clause was the 1st Linq Group Join, SQL Profiler reported an INNER JOIN. If the self-referencing LEFT JOIN clause was the LAST Linq Group Join, SQL Profiler reported an LEFT JOIN.