I'm having trouble with LINQ to SQL, which I think should not be too difficult.
In SQL I have a BusinessUnits, that get's divided in OrgUnits, and Users belong to an Org Unit.
I want to print the BusinessUnitID with the number of Users in each.
In SQL, it will probably look like this:
SELECT BusinessUnitID, Count(u.UserID)
FROM BusinessUnitsOrgUnits bu
INNER JOIN OrgUnits org on bu.OrgUnitID= org.OrgUnitID
INNER JOIN Users u on org.OrgUnitID = u.OrgUnitID
GROUP BY BusinessUnitID
But in LINQ I got this, but struggling to get the count correct.
var UsersPerBU = from bu in BusinessUnitsOrgUnits
join org in OrgUnits on bu.OrgUnitID equals org.OrgUnitID
join u in Users on org.OrgUnitID equals u.OrgUnitID
group bu by bu.BusinessUnitID into g
select new
{
BusinessUnitID = g.Key,
UserCount = Users.Count (us => us.OrgUnit.OrgUnitID == bu.OrgUnitID)
//here it complains that bu does not exist.
};
var UsersPerBU = from bu in BusinessUnitsOrgUnits
join org in OrgUnits on bu.OrgUnitID equals org.OrgUnitID
join u in Users on org.OrgUnitID equals u.OrgUnitID
group bu by bu.BusinessUnitID into g
select new
{
BusinessUnitID = g.Key,
UserCount = g.Count()
};
Maybe this
var UsersPerBU = (from bu in BusinessUnitsOrgUnits
join org in OrgUnits on bu.OrgUnitID equals org.OrgUnitID
join u in Users on org.OrgUnitID equals u.OrgUnitID
group bu by bu.BusinessUnitID into g
select new { bu = g})
.Select(x =>
new
{
BusinessUnitID = x,
UserCount = x.bu.Select(y => y.OrgUnitID).Distinct().Count()
//here it complains that bu does not exist
}
);
Related
$sql = "SELECT cc.name AS c_name, ev.name AS event_name, ev.eventModeId AS event_mode, ev.carClassHash, cc.carClassHash
FROM EVENT_DATA e
INNER JOIN PERSONA p ON e.personaId = p.ID
INNER JOIN CUSTOMCAR cc ON cc.ownedCarId = e.carId
INNER JOIN CAR_CLASSES ccs ON ccs.store_name = cc.name
INNER JOIN USER u ON u.ID = p.USERID
LEFT JOIN BAN b ON b.user_id = u.ID
INNER JOIN EVENT ev ON ev.ID = e.EVENTID
WHERE (p.name = ?
AND ev.carClassHash = cc.carClassHash)";
This query works for me except I'd also like to display any carClassHash with the value '607077938'. Is there a way I could somehow keep the above query to check if the event hash matches the car class hash but also still display values where the carClassHash (cc.carClassHash) is equal to '607077938'?
Thanks! :)
Why not use OR in the WHERE clause?
[...]
WHERE ((p.name = ? AND ev.carClassHash = cc.carClassHash) OR (cc.carClassHash = 607077938))
This should work if that value is an integer. If it's a string, use quotes around it.
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
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;
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()
{
.......
After quick overview I didn't find how to deal with linq to sql if I have several nested inner joins.
That's what I'd like to have in linq
SELECT Booking.BookingId, Booking.EventId, Booking.StartDate, Event.Name, Person.FirstName
FROM Booking
INNER JOIN Event
INNER JOIN Asset
ON Asset.AssetId = Event.AssetId
INNER JOIN Person
ON Person.PersonId = Event.ContactPersonId
ON Event.EventId = Booking.EventId AND Event.State = 4
Does anyone know how to translate it to LINQ?
Thanks.
var q1= from a in booking,b in event,c in asset, d in person where a.eventid=b.eventid and b.state=4 and c.assetid = b.assetid and b.contactpersonid=d.personid select a,b,c,d
you can replace a,b,c,d at then with the column names you want
another way is to use the join keyword:
var w1= from a in booking join b in event on a.eventid equals b.eventid join c in asset on ...
var query = from b in context.Bookings
from e in context.Events
join a in context.Assets on e.AssetId equals a.AssetId
join p in context.People on e.ContactPersonId equals p.PersonId
where e.State == (byte)States.Approved && e.EventId == b.EventId
select new { EventName = e.Name, BookingDate = b.StartDate };