Converter sql query to Linq - linq-to-sql

I need to convert this sql query to linq to sql and the result returns a IEnumerable:
select VisualAidName, v.VisualAidID, vs.VisualAidStatusName,
br.BrandName, v.IsEnabled, v.VisualAidCode, v.DateApproved,
br.BrandID, type, UserFirstName+ ' ' + UserLastName as name, AreaID
from VisualAids v inner join VisualAidStatus vs
on v.VisualAidStatusId = vs.VisualAidStatusId
inner join brands br
on v.BrandID = br.BrandId
inner join VisualAids_Areas_Link vareas
on v.VisualAidID = vareas.VisualAidID
left join users us
on v.Owner = us.UserID
where
AreaID IN (
select areaid
from Users inner join Users_Area_Link
on Users.UserID = Users_Area_Link.UserID
where Users.UserID= 3
)
I did this:
IEnumerable<Visual_Aid> visualAll = from v in Context.VisualAids
join vs in Context.VisualAidStatus on v.VisualAidStatusId equals vs.VisualAidStatusId
join br in Context.Brands on v.BrandID equals br.BrandId
join us in Context.Users on v.Owner equals us.UserID into vadis
from x in vadis.DefaultIfEmpty()
select new Visual_Aid()
{
VisualAid_Name = v.VisualAidName,
VisualAid_Id = v.VisualAidID,
VisualAid_StatusName = vs.VisualAidStatusName,
VisualAid_BrandsName = br.BrandName,
VisualAid_IsEnabled = bool.Parse(v.IsEnabled.ToString()),
VisualAid_Code = v.VisualAidCode,
VisualAid_DateApp = v.DateApproved.ToString() ?? "",
VisualAid_BrandId = int.Parse(v.BrandID.ToString()),
VisualAid_Type = v.Type,
VisualAid_Owner = x.UserID == null ? "" : x.UserFirstName + " " + x.UserLastName
};
but I need to do the part of the subquery, ie, I need to include this:
where AreaID IN (
select areaid from Users inner join Users_Area_Link
on Users.UserID = Users_Area_Link.UserID where Users.UserID= 3
)
Anybody know how? thank you very much in advance

You can add this as a where statement:
.......
join us in Context.Users on v.Owner equals us.UserID into vadis
from x in vadis.DefaultIfEmpty()
where (
from user in Context.Users
join userArea in Users_Area_Link
on user.UserID equals userArea.UserID
where user.UserID==3
select userArea.areaid
).Contains(????.AreaID)
select new Visual_Aid()
{
.......

Related

MySQL: From sub query to a single query

I have this query which i believe can be optimized:
SELECT floors.id, floors.floor FROM floors
WHERE floors.societies_id = 1
AND floors.status = 'Y'
AND floors.id NOT IN (
SELECT DISTINCT(floors.id) FROM floors
INNER JOIN societies ON societies.id = floors.societies_id
INNER JOIN resident_floors ON resident_floors.floors_id = floors.id
WHERE societies.id = 1
AND floors.status = 'Y'
)
Is this query fine to use or there it can be improved..?
It looks like you want to get all floors that aren't present in resident_floors. For this we can left join RF in and ask for only rows where the join failed resulting in a null in RF:
SELECT floors.* FROM floors
INNER JOIN societies ON societies.id = floors.societies_id
LEFT JOIN resident_floors ON resident_floors.floors_id = floors.id
WHERE societies.id = 1
AND floors.status = 'Y'
AND resident_floors.floors_id IS NULL

Join the same table thrice and retrieve a column from those

SELECT DISTINCT
,PH.PHONE_TYPE_CD
,PH.PHONE_NUM
FROM PERSON P
LEFT JOIN PHONE PH
ON PH.PARENT_ENTITY_ID = P.PERSON_ID
AND PH.PARENT_ENTITY_NAME = 'PERSON'
AND PH.PHONE_TYPE_CD = ?
AND PH.ACTIVE_IND = 1
LEFT JOIN PHONE PH
ON PH.PARENT_ENTITY_ID = P.PERSON_ID
AND PH.PARENT_ENTITY_NAME = 'PERSON'
AND PH.PHONE_TYPE_CD = ?
AND PH.ACTIVE_IND = 1
LEFT JOIN PHONE PH
ON PH.PARENT_ENTITY_ID = P.PERSON_ID
AND PH.PARENT_ENTITY_NAME = 'PERSON'
AND PH.PHONE_TYPE_CD = ?
AND PH.ACTIVE_IND = 1
Here PHONE_TYPE_CD will be passed from the Java side and based on the PHONE_TYPE_CD, the query should run and return results.
Since I am new to SQL, I am not sure how to achieve this. I understand all the 3 joins should have aliases like PHONE PH1, PHONE PH2 and so on.. My question is can I code like below and get the PHONE_NUM based on the passed PHONE_TYPE_CD:
SELECT DISTINCT
,PH1.PHONE_TYPE_CD
,PH1.PHONE_NUM
,PH2.PHONE_TYPE_CD
,PH2.PHONE_NUM
,PH3.PHONE_TYPE_CD
,PH3.PHONE_NUM
FROM PERSON P
LEFT JOIN PHONE PH1
ON PH1.PARENT_ENTITY_ID = P.PERSON_ID
AND PH1.PARENT_ENTITY_NAME = 'PERSON'
AND PH1.PHONE_TYPE_CD = ?
AND PH1.ACTIVE_IND = 1
LEFT JOIN PHONE PH2
ON PH2.PARENT_ENTITY_ID = P.PERSON_ID
AND PH2.PARENT_ENTITY_NAME = 'PERSON'
AND PH2.PHONE_TYPE_CD = ?
AND PH2.ACTIVE_IND = 1
LEFT JOIN PHONE PH3
ON PH3.PARENT_ENTITY_ID = P.PERSON_ID
AND PH3.PARENT_ENTITY_NAME = 'PERSON'
AND PH3.PHONE_TYPE_CD = ?
AND PH.ACTIVE_IND = 1
I have ambiguity regarding the retrieval part.
You can do this:
SELECT DISTINCT
,PH1.PHONE_TYPE_CD
,PH1.PHONE_NUM
,PH2.PHONE_TYPE_CD
,PH2.PHONE_NUM
,PH3.PHONE_TYPE_CD
,PH3.PHONE_NUM
FROM PERSON P
LEFT JOIN PHONE PH1
ON PH1.PARENT_ENTITY_ID = P.PERSON_ID
AND PH1.PARENT_ENTITY_NAME = 'PERSON'
AND PH1.PHONE_TYPE_CD in (type1, type2, type3)
AND PH1.ACTIVE_IND = 1

SQL passing table column to a function parameter

I wish to call my function (dbo.Split) with a column table (U.UbicacionF) but it doesn't work! Please can u help me!! Thanks
SELECT
EE.IDEstatus, EE.IDAsignacion,EE.IDUNIDAD,
EE.IDEQUIPO,EE.ESTATUS,EE.bUltEstatus,
A.IDDestino, U.UbicacionF, TI.Tiempo
FROM
mar_EstatusEquipo EE
LEFT JOIN
mar_Asignaciones A ON EE.IDAsignacion = A.IDAsignacion
AND EE.IDUNIDAD = A.IDUNIDAD
LEFT JOIN
mar_TmpUbicaciones U ON EE.IDUNIDAD = U.IdUnidad AND U.IdUnidad = A.IDUNIDAD
LEFT JOIN
mar_TiemposArriboPto TI ON A.IDDestino = TI.Destino
AND TI.Ubicacion LIKE '%' + (SELECT *
FROM dbo.Split(U.UbicacionF, ',', '1')) + '%'
WHERE
EE.IDEquipo = CAST(EE.IDUNIDAD AS CHAR)
AND EE.Estatus IN ('IT','ET')
AND A.IDDestino = 'MZO'
AND EE.bUltEstatus = 1
AND EE.IDUNIDAD = 255
ORDER BY
EE.IDUNIDAD
You could use an APPLY operator to pass a column to a table-valued function like that. In your case it would rather be OUTER APPLY than CROSS APPLY, to match your current outer join logic:
SELECT
EE.IDEstatus, EE.IDAsignacion,EE.IDUNIDAD,
EE.IDEQUIPO,EE.ESTATUS,EE.bUltEstatus,
A.IDDestino, U.UbicacionF, TI.Tiempo
FROM
dbo.mar_EstatusEquipo EE
LEFT JOIN
dbo.mar_Asignaciones A ON EE.IDAsignacion = A.IDAsignacion
AND EE.IDUNIDAD = A.IDUNIDAD
LEFT JOIN
dbo.mar_TmpUbicaciones U ON EE.IDUNIDAD = U.IdUnidad AND U.IdUnidad = A.IDUNIDAD
OUTER APPLY (
SELECT t.Tiempo
FROM
dbo.mar_TiemposArriboPto AS t
INNER JOIN
dbo.Split(U.UbicacionF, ',', '1') AS s ON t.Ubicacion LIKE '%' + s.Value + '%'
) AS TI
WHERE
EE.IDEquipo = CAST(EE.IDUNIDAD AS CHAR)
AND EE.Estatus IN ('IT','ET')
AND A.IDDestino = 'MZO'
AND EE.bUltEstatus = 1
AND EE.IDUNIDAD = 255
ORDER BY
EE.IDUNIDAD
;

MySQL Unknown column u.id in on clause but column exists

I've got this tables:
users {id = int, name = varchar, pwd = char}
company {id = int, token = char, name = varchar}
user_company {id = int, id_usr = int, id_company = int, name_usr = varchar}
I'm trying to get the pwd from users and find out if the user is in the company with the token X from user_company
When I use this query
SELECT u.pwd,h.name_usr
FROM users u, company c
LEFT JOIN users_company h ON c.id = h.id_company AND u.id = h.id_usr
WHERE u.user_name = 'user#domain.com'
AND c.token = 'f30ea71e7a9d9f0a6710bb46537c0bde'
LIMIT 1;
I keep on getting 'Unknown column u.id in on clause' although u.id exists. What am I doing wrong? Thanks
SELECT u.pwd,h.nombre_usr
FROM company c LEFT JOIN users_company h ON c.id = h.id_company
LeFt join users u on u.id = h.id_usr
WHERE u.user_name = 'user#domain.com'
AND c.token = 'f30ea71e7a9d9f0a6710bb46537c0bde'
LIMIT 1;
The Error was beacause of Join Query . The way it was written is wrong. We can not apply join two tables with single table at same time.
sI've got the solution. Thank you all for the help.
SELECT u.pwd,h.name_usr
FROM users u
LEFT JOIN company c ON c.token = 'f30ea71e7a9d9f0a6710bb46537c0bde'
LEFT JOIN users_company h ON c.id = h.id_company AND u.id = h.id_usr
WHERE u.user_name = 'user#domain.com'
LIMIT 1;

OR opertor ignores AND operator MYSQL

see the query below i want all those records where status is either using or offering and userid is not equals to 1 and coursecode is cs3333
SELECT c.`id` AS courseid,
c.`userid` AS userid,
c.`coursecode`,
m.`title`,
s.`sem_name`,
p.`professor`,
st.`status`,
a.`author_name`,
q.`quality`,
m.`comments`,
m.`price`,
m.`material`
FROM sc_courses c
JOIN sc_c_materials m
ON c.`id`=m.`courseid`
JOIN sc_semesters s
ON s.`id`=c.`semid`
JOIN sc_professors p
ON c.`profid`=p.`id`
JOIN sc_status st
ON c.`statusid`=st.`id`
LEFT JOIN sc_authors a
ON m.`authorid`=a.`id`
JOIN sc_quality q
ON m.`qualityid`=q.`id`
WHERE st.`status` = "offering" OR st.`status` = "using" AND c.`userid` != "1" AND c.`coursecode` = "CS3333";
the query is running but it ignores both the and operators the above query return all records where userid is 1 and coursecode is not equal to cs3333 but i dont want these records plz tell me what am i doing wrong????
You just need brackets
WHERE (st.`status` = "offering" OR st.`status` = "using") AND c.`userid` != "1" AND c.`coursecode` = "CS3333";
That is because priority. You should place ( .. ) to change order of comparison.
(st.`status` = "offering" OR st.`status` = "using")
AND c.`userid` != "1"
AND c.`coursecode` = "CS3333"
opss stupid mistake here it is working with this query
SELECT c.`id` AS courseid,
c.`userid` AS userid,
c.`coursecode`,
m.`title`,
s.`sem_name`,
p.`professor`,
st.`status`,
a.`author_name`,
q.`quality`,
m.`comments`,
m.`price`,
m.`material`
FROM sc_courses c
JOIN sc_c_materials m
ON c.`id`=m.`courseid`
JOIN sc_semesters s
ON s.`id`=c.`semid`
JOIN sc_professors p
ON c.`profid`=p.`id`
JOIN sc_status st
ON c.`statusid`=st.`id`
LEFT JOIN sc_authors a
ON m.`authorid`=a.`id`
JOIN sc_quality q
ON m.`qualityid`=q.`id`
WHERE c.`userid` != "1" AND c.`coursecode` = "CS3333" AND st.`status` = "offering" OR
c.`userid` != "1" AND c.`coursecode` = "CS3333" AND st.`status` = "using";