I want to convert LINQ to SQL to SQL query. I know simple LINQ to SQL but don't know about more.
Given below is my Linq to Sql and I want to convert it into Sql query.
from objSql in objContext.DoctorNotes
join objCreatedU in objContext.Users on objSql.CreatedByUserFK equals objCreatedU.UserID into objCU
from tblC in objCU.DefaultIfEmpty()
join objModifiedU in objContext.Users on tblC.ModifiedByUserFK equals objModifiedU.UserID into objMU
from tblM in objMU.DefaultIfEmpty()
select new DoctorNoteBind(objSql)
{
CreatedBy = tblC.UserName,
ModifiedBy = tblM.UserName
}).ToList();
The .DefaultIfEmpty() are used to represent LEFT JOINs (http://msdn.microsoft.com/en-us/library/ms187518(v=sql.100).aspx). You'd expect the equivalent SQL to take the following form:
SELECT
U1.UserName AS CreatedBy,
U2.UserName AS ModifiedBy
FROM
DoctorNotes DN
LEFT JOIN Users U1 ON
U1.UserID = DN.CreatedByUserFK
LEFT JOIN Users U2 ON
U2.UserID = DN.ModifiedByUserFK
-- WHERE ?
-- ORDER BY ?
Related
I have an linq query like this :
var query = from Romm in RoMM
join rfrsa in RoMmfrsa on Romm.RoMmid equals rfrsa.RoMmid
join frsa in Frsa on rfrsa.Frsaid equals frsa.Fraid
join fra in Fra on frsa.Fraid equals fra.Fraid
where Romm.ActTypeId == 2 && Romm.SegmentId == 4
select new
{
Romm.ActTypeId,
Romm.RoMmid,
frsa.Fraid,
frsa.Frsaid,
Romm.ImpactId
};
And I have SQL code as below :
SELECT romm.ROMMID
, frsa.FRAID
, frsa.FRSAID
, romm.ImpactID
FROM RoMM AS romm
INNER
JOIN RoMMFRSA AS rfrsa
ON romm.RoMMID = rfrsa.RoMMID
INNER
JOIN FRSA AS frsa
ON rfrsa.frsaid = frsa.frsaid
INNER
JOIN FRA AS fra
ON frsa.FRAID = fra.FRAID
WHERE romm.acttypeid = 2
AND romm.segmentid = 4
The SQL only shows one row (which is correct), the linq shows the correct row and then it displays about another 3 rows which is not what we need. I need the linq to show one row which is correct with the SQL. Is this because of maybe many-many relationships ?
Looks like a typo in either the C# or the SQL join:
SQL: ON rfrsa.frsaid = frsa.frsaid
C#: rfrsa.Frsaid equals frsa.Fraid
^^^^^^
mismatch here
I want convert following LINQ query to SQL query.
var ACTIVITY_ROYALITY_MONTH = from m in db.MiningPermit
join pm in db.Permit_Mineral on m.MINING_PERMIT_ID equals pm.MINING_PERMIT_ID
join r in db.Royality on pm.Permit_Minerals_ID equals r.Permit_Minerals_ID
where r.ORDER_ID == 0 // NULL in server
orderby r.YEAR, r.MONTH
group r by new { m.MINING_PERMIT_ID , r.YEAR, r.MONTH } into mpmr
select mpmr.ToList();
Use Linqpad and recreate the linq (even by bringing in your assemblies) in a C# query. Run the query. Then in the output, there is a selection button of SQL which will show the sql code.
I need your help because I'm trying to translate a very specific SQL query to Linq but I can't do it by myself because of an ISNULL clause that I don't know how to put it in LINQ. Here is the SQL query:
select f.fId as alias1, f.fDate, f.IniHour, f.EndHour, f.Cap, f.Ocup
, isnull(
(select o.Cap - o.Ocup
from table2 o
left outer join table3 r on o.id = r.id
where o.fId=f.id and r.fieldI = #value)
, 1) as alias2
from table1 f
inner join table4 t on f.id=t.id
and f.fDate+cast(fIniHour as datetime) > Dateadd(Hour,2,Getdate())
order by f.fDate,f.IniHour
The problem is how to put the ISNULL in the Select clause in the Linq query. Could you, please, help me with this issue?
Thank you in advanced.
I'm trying to get this query to work but I just can't. I'm quite new to entity and linq. Thanks
select distinct
Usuario.idUsuario,
Usuario.Nombre,
Usuario.DNI
from
Usuario
right join
Relaciones on (Usuario.idUsuario = Relaciones.idUsuario)
inner join
Cursos on (Relaciones.idCurso = Cursos.idCurso)
This is what I tried so far
var query = from Usuario in db.Usuario
from Relaciones in db.Relaciones
where Relaciones.Cursos.idCurso == id
select distinct Usuario;
but the distinct in select usuario shoots me an error
To emulate the distinct sql clause using linq you need to use the distinct method which returns distinct elements from a sequence by using the default equality comparer to compare values.
var query = (from Usuario in db.Usuario
join Relaciones in db.Relaciones on Usuario.idUsuario = Relaciones.idUsuario
join Cursos in db.Cursos on Relaciones.idCurso = Cursos.idCurso
where Cursos.idCurso == id
select Usuario).Distinct();
This might be one of those situations where plain SQL commands are better than LINQ. Here's a simplified version of the SQL statement I'm trying to translate:
SELECT * FROM IDTable AS idt
INNER JOIN NameTable AS nt ON nt.IDTableID=idt.Id
AND nt.Id= (SELECT TOP(1) Id
FROM NameTable AS nt2
WHERE nt2.IDTableID=11 ORDER BY nt2.DateInserted DESC)
I have the LINQ query to pull records when just joining on IDs and I've seen how to join on multiple columns, but I have no idea how to plug the subquery into the mix.
If this isnt entirely clear, please let me know and I'll edit to elaborate.
Maybe something like this?
var results = from id in db.IDTable
join n in db.NameTable on id.Id equals n.IDTableID
where n.Id = (
from n2 in db.NameTable
where n2.IDTableID = 11
orderby n2.DateInserted desc
).First()
select new { id, n };