How can I write a fluent nhibernate query for the below SQL query statement :
select iseat.companynr, mdata.Employeenr from IDepartment as iseat inner join
Employee as i on i.pkey = iseat.employeefk
inner join employeedata as edata on edata.employeefk = i.pkey
I have already created classes IDepartment , Employee and Employeedata and also I have created a class CompanyNr2EmployeeNrMap , which contains 2 properties CompanyNr and EmployeeNr.
The query needs to take a list of companynr as parameters and return a list of CompanyNr2EmployeeNrMap.
var query = from emp in session.Query<Employee>()
from department in emp.Departments
select new CompanyNr2EmployeeNrMap
{
CompanyNr = department.CompanyNr,
EmployeeNr = emp.Data.EmployeeNr
};
return query.ToList();
Related
Here is my SQL-Query that I want to convert to LINQ:
SELECT artikel.id, charge.id
from Lots charge
JOIN Product artikel on charge.artikel = artikel.id
JOIN StorageQuantity_TAB lmengeTAB on charge.id = lmengeTAB.charge
JOIN StorageQuantity lmenge on lmengeTAB.id = lmenge.id
GROUP BY artikel.id, charge.id
That´s what I got so far:
var query = (from charge in Lots
join artikel in PartProducts on charge.Artikel equals artikel.Id
join lmengeTAB in StorageQuantity_TABs on charge.Id equals lmengeTAB.Charge
join lmenge in StorageQuantities on lmengeTAB.Id equals lmenge.Id
select new
{
artikel.Id,
chargeID = charge.Id,
}).ToList();
So how do I include that GROUP BY statement?
I'm trying to convert a SQL statement to LINQ and for some reason the result sets I'm getting are different. The SQL is returning more data than the LINQ.
This is the SQL Query:
SELECT DISTINCT ssLookup.StopID,
dc.CityName,
dc.StateAbbrev,
sc.CarrierID,
sc.CarrierCode,
sc.CarrierName
FROM ScheduleDestinationCache dc
INNER JOIN ScheduleStops ssFilter ON ssFilter.CarrierID = dc.CarrierID
INNER JOIN ScheduleStops ssLookup ON dc.CityID = ssLookup.StopID
INNER JOIN ScheduleCarriers sc ON ssLookup.CarrierID = sc.CarrierID
WHERE (ssFilter.StopID = 582 OR ssFilter.StopID IN
(SELECT * FROM dbo.GetTwins(582)))
ORDER BY dc.CityName, dc.StateAbbrev
This is the LINQ statement:
(from sdc in ScheduleDestinationCaches
let twins = GetTwins(582).Select(gt => gt.StopIDs)
join ssFilter in ScheduleStops on sdc.CarrierID equals ssFilter.CarrierID
join ssLookup in ScheduleStops on sdc.CityID equals ssLookup.CityID
join sc in ScheduleCarriers on ssLookup.CarrierID equals sc.CarrierID
where ssFilter.StopID == 582 || twins.Contains(ssFilter.StopID)
orderby sdc.CityName, sdc.StateAbbrev
select new {
ssLookup.StopID,
sdc.CityName,
sdc.StateAbbrev,
sc.CarrierID,
sc.CarrierCode,
sc.CarrierName
}).GroupBy(g => new
{
g.StopID,
g.CityName,
g.StateAbbrev,
g.CarrierID,
g.CarrierCode,
g.CarrierName
})
What am I doing wrong?
Thank you in advance.
I am trying to 'inner join' two table values parameters but when i execute the query it takes for ever and i have to stop debug in ms sql server management studio 2008.
The query:
#allClients [dbo].[ImportedClients] READONLY,
#NewClients [dbo].[ImportedClients] READONLY
--Update Clients table in db with new clients
UPDATE [dbo].[Clients]
SET
Email = ISNULL(ct.Email, x.Email),
FirstName = ISNULL(ct.FirstName, x.FirstName),
LastName = ISNULL(ct.LastName, x.LastName),
Telephone = ISNULL(ct.Telephone, x.Telephone),
Cellphone = ISNULL(ct.Cellphone, x.Cellphone),
FROM #NewClients ct
inner join #allClients x
ON (x.Email = ct.Email)
WHERE ct.Status > -1
What i am trying to do is: Update all the clients information for rows that exist in both #NewClients and #allClients so i used inner join.
Is it possible to JOIN two table-Valued Parameters?
This query doesn't (query doesn't complete without an error) work even with a few rows.
any suggestions?
You need to reference [dbo].[Clients] in the FROM clause of your SQL.
Something like this:
#allClients [dbo].[ImportedClients] READONLY,
#NewClients [dbo].[ImportedClients] READONLY
--Update Clients table in db with new clients
UPDATE [dbo].[Clients]
SET
Email = ISNULL(ct.Email, x.Email),
FirstName = ISNULL(ct.FirstName, x.FirstName),
LastName = ISNULL(ct.LastName, x.LastName),
Telephone = ISNULL(ct.Telephone, x.Telephone),
Cellphone = ISNULL(ct.Cellphone, x.Cellphone),
FROM [dbo].[Clients] c
INNER JOIN #NewClients ct ON (c.Email = ct.Email)
INNER JOIN #allClients x ON (x.Email = ct.Email)
WHERE ct.Status > -1
Given three tables:
People: {Person_Id, Age}
Accounts: {Account_Id, Person_Id, Account_Type}
Age_Ranges: {Age_Range_Id, Age_Range_Label, Lower_Bound, Upper_Bound}
I'm trying to represent the following SQL query using OJB (without resorting to a QueryBySQL):
select ar.Age_Range_Label, count(a.Account_Id)
from People p
inner join Accounts a on p.Person_Id = a.Person_Id
inner join Age_Ranges ar on p.Age between ar.Lower_bound and ar.Upper_Bound
where a.Account_Type = 'Chequing'
The java code I have so far looks like this:
Criteria criteria = new Criteria();
criteria.addEqualTo("Account_Type", "Chequing");
// join on Age_Ranges based on the person's Age
criteria.???
ReportQueryByCriteria q = QueryFactory.newReportQuery(Accounts.class, criteria);
q.setAttributes(new String[] {"Age_Range_Label", "count(Account_Id)"});
q.addGroupBy("Age_Range_Label");
...
But I'm unsure how to join up with the Age_Ranges table such that I can group on the Age_Range_Label column.
Any insight?
I use Zend_Db_Select to perform a query with a Join. I end up with the following SQL query :
SELECT `Utilisateur`.*, `Ressource`.*, `Acl_Cache`.*, `Role`.*, `UtilisateurRole`.* FROM `Utilisateur`
INNER JOIN `Ressource` ON Ressource.idJointure = Utilisateur.id
INNER JOIN `Acl_Cache` ON Acl_Cache.idRessource = Ressource.id
INNER JOIN `Role` ON Role.id = Acl_Cache.idRole
INNER JOIN `UtilisateurRole` ON UtilisateurRole.idRole = Role.id
WHERE (Ressource.tableJointure = 'Utilisateur') AND (UtilisateurRole.idUtilisateur = '2')
Why is Zend_Db adding this part is the SELECT clause :
, `Ressource`.*, `Acl_Cache`.*, `Role`.*, `UtilisateurRole`.*
I never asked this, and I don't want that. How to prevent this behavior ?
$db->select()
->from(array('alias1'=>'table_i_want_all_cols_on'))
->joinLeft(array('alias2'=>'table_i_want_no_cols_on'),
'alias1.id = alias2.id',
array()
)
->joinLeft(array('alias3'=>'table_i_want_some_cols_on'),
'alias3.id = alias1.id',
array('col1', 'col2')
);