Convert SQL select statement to Linq - linq

How can I convert the following SQL select statement to Linq?
SELECT u.Name FROM User u AS DDC
INNER JOIN Country c ON c.UserId = u.UserId
INNER JOIN (
SELECT AddressId,
Address,
PC,
FROM AddressTbl a
WHERE a.CountryId = 1
) AS Addresses ON Addresses.AddressId= u.AddressId
WHERE
u.UserIs = #UserId AND
Addresses.AddressId= #AddressId
Any good reading references?

from u in Users join
c in Country on c.UserId equals u.UserId
join a in Address on a.AddressId equals u.AddressId
where a.CountryId == 1
select u.Name

You can try this out
http://www.sqltolinq.com/

Related

How can I solve the ORA-01799 when creating a VIEW with a LEFT OUTER JOIN and a MAX value?

I am trying to create a view that joins a few tables, one of the joins is using a max value and I think that is generating the following error in our Oracle DB:
ORA-01799: a column may not be outer-joined to a subquery.
Can anyone help me to change the estatement to create the view? The query works fine, the problem only comes when I try to create a view with it.
CREATE or replace VIEW xxxxxx_V (ID, SERVICE_ID, PARENT_ID, PARENT_MATERIALITY, PARENT_END_STATUS,
PARENT_OUTS_TYPE, END_STATUS, MATERIALITY, OUTS_TYPE, CREATED_BY,
SERVICE_DESCRIPTION_SHORT, VERSION, OUTSOURCER_ID, OUTSOURCER_NAME, INSOURCER_ID, INSOURCER_NAME, PLANNED_SERVICE_START) AS
select c.id id, s.id service_id, s.outsourcing_id parent_id, p.materiality parent_materiality, p.end_status parent_end_status,
p.outsourcing_type parent_outs_type, c.end_status end_status, c.materiality materiality, c.outsourcing_type outs_type, c.created_by created_by,
r.service_description_short, r.version, r.outsourcer outsourcer_id, o.name outsourcer_name , r.insourcer insourcer_id,i.name insourcer_name, r.planned_service_start
from OS_OUTSOURCING_CONTRACT c
left join OS_SERVICES s on s.id = c.service_id
left join OS_OUTSOURCING_CONTRACT p on p.id = s.outsourcing_id
left join OS_RISK_ASSESSMENT r on r.outsourcing_id = c.id and r.version = (select max(version) from OS_RISK_ASSESSMENT where outsourcing_id = c.id)
left join OS_COMPANY_INSOURCER i on i.id = r.insourcer
left join OS_COMPANY_OUTSOURCER o on o.id = r.outsourcer
Join the subquery into the query:
CREATE or replace VIEW xxxxxx_V
(ID, SERVICE_ID, PARENT_ID, PARENT_MATERIALITY, PARENT_END_STATUS,
PARENT_OUTS_TYPE, END_STATUS, MATERIALITY, OUTS_TYPE, CREATED_BY,
SERVICE_DESCRIPTION_SHORT, VERSION, OUTSOURCER_ID, OUTSOURCER_NAME,
INSOURCER_ID, INSOURCER_NAME, PLANNED_SERVICE_START) AS
select c.id,
s.id service_id,
s.outsourcing_id parent_id,
p.materiality parent_materiality,
p.end_status parent_end_status,
p.outsourcing_type parent_outs_type,
c.end_status,
c.materiality,
c.outsourcing_type outs_type,
c.created_by,
r.service_description_short,
r.version,
r.outsourcer outsourcer_id,
o.name outsourcer_name,
r.insourcer insourcer_id,
i.name insourcer_name,
r.planned_service_start
from OS_OUTSOURCING_CONTRACT c
LEFT OUTER JOIN (select outsourcing_id,
max(version) AS MAX_VERSION
from OS_RISK_ASSESSMENT
group by outsourcing_id) ora
ON ora.OUTSOURCING_ID = c.id
left join OS_SERVICES s
on s.id = c.service_id
left join OS_OUTSOURCING_CONTRACT p
on p.id = s.outsourcing_id
left join OS_RISK_ASSESSMENT r
on r.outsourcing_id = ora.OUTSOURCING_ID and
r.version = ora.MAX_VERSION
left join OS_COMPANY_INSOURCER i
on i.id = r.insourcer
left join OS_COMPANY_OUTSOURCER o
on o.id = r.outsourcer

How count table a data for every data of table a

I have three table A,B,C.
A table:
id,name
B table:
id,a_id,date
C table:
id,b_id,type(value is 0/1)
I want to print all A.name,A.id and C.countingdata by counting C data where C.type=1 using B table which has A table id
Result look like below:
A.id A.name C.countingdata
1 abc 4
2 vfd 2
3 fdg 0
Well, you can first inner join B and C, do the group by and get C.countingdata using count(). Another join on this subquery with B itself to accommodate the a_id
in the result set.
Now, you can do an inner join between A and the above subquery to get your results.
SQL:
select A.id, A.name, derived.countingData
from A
inner join (
select B.id as b_id,B.a_id,sub_data.countingData
from B
inner join (
select B.id,count(B.id) as countingData
from B
inner join C
on B.id = C.b_id
where C.type=1
group by B.id
) sub_data
on B.id = sub_data.id
) derived
on A.id = derived.a_id
You can find query as below:
Select
A.id
,A.name
,COUNT(C.id)
FROM A
JOIN B ON A.id = B.a_id
JOIN C ON B.id = C.b_id ANd C.type = 1
GROUP BY
A.id
,A.name

Convert this sql command to c# linq

I am trying to convert this tSql command to linq query.
I want to group this columns.
Can you help me?
select vUnit.FK_Unit_ID , Unit.unitNumber, unit.unitTitle , title.featureTitleName
from unit.UnitFeatureValue vUnit
inner join unit.Unit on vUnit.FK_Unit_ID = Unit.ID
inner join unit.FeatureTitle title on vUnit.FK_FeatureTitle_ID = title.ID
where vUnit.FK_Unit_ID = 15 and title.canMoreSelect = 1
group by vUnit.FK_Unit_ID ,unit.unitNumber, unit.unitTitle , title.featureTitleName
var query = (
from v in dbContext.UnitFeatureValue
join u in dbContext.Unit on v.FK_Unit_ID equals u.ID
join t in dbContext.FeatureTitle on v.FK_FeatureTitle_ID equals t.ID
where v.FK_Unit_ID == 15 && t.canMoreSelect == 1
select new {
v.FK_Unit_ID,
u.unitNumber,
u.unitTitle,
t.featureTitleName,
}).Distinct();
something like this
var result = (from v in vUnit
join u in unit on v.FK_Unit_ID equals u.ID
join t in title on v.FK_FeatureTitle_ID equals t.ID
where v.FK_Unit_ID == 15 and t.canMoreSelect == 1
select new { v.FK_Unit_ID , u.unitNumber, u.unitTitle , t.featureTitleName }).ToList();

ORA-00979: not a GROUP BY expression while querying a View with a subquery

I have a view as follows - It compiles just fine but when I try to select from it, I get a ORA-00979: not a GROUP BY expression error. If I eliminate the subquery (column 4) from the view, all works fine. Any ideas would be greatly appreciated! Thanks!!
select
l.LAB_GROUP,
l.NAME as LAB,
b.NAME as BENCH,
(select count(distinct s2.SAMPLE_NUMBER)
from SAMPLE s2 inner join TEST t2 on s2.SAMPLE_NUMBER = t2.SAMPLE_NUMBER and t2.STATUS in ('C', 'R') and s2.TEMPLATE <> 'QC_SAMPLE'
inner join LABORATORY_ENTRY le2 on t2.ANALYSIS = le2.ANALYSIS
where s2.LAB_GROUP = l.LAB_GROUP and le2.NAME = l.NAME and t2.X_BENCH = b.NAME and
((select count(t1.TEST_NUMBER)
from TEST t1
where t1.SAMPLE_NUMBER = t2.SAMPLE_NUMBER and t1.ANALYSIS = t2.ANALYSIS and t1.STATUS <> 'R') = 0)) as RFR
from LABORATORY l
inner join LABORATORY_ENTRY le on le.NAME = l.NAME
inner join X_BENCH b on b.NAME = le.X_BENCH
left join (SAMPLE s inner join TEST t on s.SAMPLE_NUMBER = t.SAMPLE_NUMBER and s.STATUS <> 'U'
and s.TEMPLATE <> 'QC_SAMPLE' and t.STATUS in ('I', 'P')) on t.ANALYSIS = le.ANALYSIS and s.LAB_GROUP = l.LAB_GROUP
left join V_LOC_DEPT_FAC ldf on ldf.LOCATION_NUMBER = s.STORAGE_LOC_NO
group by l.LAB_GROUP, l.NAME, b.NAME
If you need to use the group by (which will be the case if you add aggregating functions, but not as the query is currently written) you need to include the subquery in the group by as well. You can add this easiest by adding a SELECT outside your main query and appyling the GROUP BY at that level:
select lab_group, lab, bench, rfr
from
(
select
l.LAB_GROUP as lab_group,
l.NAME as LAB,
b.NAME as BENCH,
(select .....) as RFR
from LABORATORY l
inner join LABORATORY_ENTRY le on le.NAME = l.NAME
inner join X_BENCH b on b.NAME = le.X_BENCH
left join (SAMPLE s inner join TEST t on ...) on
t.ANALYSIS = le.ANALYSIS and
s.LAB_GROUP = l.LAB_GROUP
left join V_LOC_DEPT_FAC ldf on ldf.LOCATION_NUMBER = s.STORAGE_LOC_NO
) x
group by lab_group, lab, bench, rfr
Try removing the...
group by l.LAB_GROUP, l.NAME, b.NAME
As I don't think this is neccesary if you're doing the count within the subquery.
However, for performance reasons I would suggest rewriting your query so that you move the subquery into the FROM section rather than the SELECT one.

Oracle parameter to a inner query

I'm struggling with an Oracle query. I want to send a value to a inner query but I don't succeed. My query looks as follows:
SELECT * FROM Pro u
LEFT JOIN (SELECT * FROM PROLOG d
WHERE d.Id = (SELECT MAX(Id) FROM PROLOG t
WHERE t.Project = **u.Id**
AND t.Prodstatus IN (5,40)))z ON (u.ID = z.Project)
WHERE u.Id = 22;
I want to replace the u.Id with 22. The value 22 comes from Pro u table. Please give me some hints.
I do not believe you can correlate a join to an inline view, however you can simplify your SQL statement to a simple outer join with a correlated predicate:
SELECT *
FROM pro u
LEFT OUTER JOIN prolog d
ON d.project = u.id
AND d.Id = (SELECT MAX(Id)
FROM prolog t
WHERE t.project = u.id
AND t.prodstatus IN (5,40))
WHERE u.id = 22;
You don't need to pass that value to sub query from Pro table, You can use prolog table value
SELECT * FROM Pro u
LEFT JOIN (SELECT * FROM PROLOG d
WHERE d.Id = (SELECT MAX(Id) FROM PROLOG t
WHERE t.Project = d.Project
AND t.Prodstatus IN (5,40)))z ON (u.ID = z.Project)
WHERE u.Id = 22;

Resources