I am writing my sub query using linq in my application. I wrote my sql sub-query in sql server. When I execute this query in SQL Server the result give perfect and the sql query will be like
My SQL Server:
select Row_Number() Over(OrderBy Mmname) as Sno,Mmname,Mmcardno,Mmdob,MmEmail,(Select SUM(MSSRNETAMT) from MSCAS where MSCAS.MSSFORMNO = MSMEM.MmCardno and MSCAS.MSSRBILLDT between '01/01/2016' and '30/12/2016') Billopen,(Select SUM(MSSRNETAMT) from MSCAS where MSCAS.MSSFORMNO = MSMEM.MmCardno and MSCAS.MSSRBILLDT between '01/01/2016' and '30/12/2016') BillForm,Mmredpv,Msdval,Mmcontact,Mmdate,Mmaddr,MmCuser,MmCuserdt,Mmusercd,
Mmuser,Mmuserdt,Cast(Mmcntrn as int ) as Mmcntr from MSMEM inner join MSSCHEDET on MSMEM.Mschuid = MSSCHEDET.MSDID
My linq query
Var query = (from Ms in db.msmems join Mss in db. Msschedet on Ms. Mechuid equals mss. MSDID select new
{
name = Ms. Mmname,
Billopen = (from mc in db. MSCAS where Ms. Mmcardno = mc. Mssformno) select mc.MSSRNETAMT);
}):
This only I tried but I am getting error in bill open it is amount in that I am getting error
This is my SQL query. I tried to convert this sql query in linq. But I am failed many times, I got an error.
Could you try something like this? Without understanding your table structure better, I took a shot at optimizing this. Is there no reason that you couldn't do a second join to your MSCAS table?
var query = (from Ms in db.msmems
join Mss in db.Msschedet on Ms.Mechuid equals mss.MSDID
join Mc in db.MSCAS on Ms.Mmcardno equals Mc.Mssformno
select new {
name = Ms.Mmname,
Billopen = Mc.where(x=>x.MSSRBILLDT >= '01-01-2016' and x.MSSRBILLDT <= '12-30-2016').Sum(z=>z.MSSRNETAMT)
}
);
Related
I am new to building sql queries and could use some help. I built a query that works fine as a standalone query. The problem is I need to use it in a report using ExecuteScalar function and nested queries are not allowed, I tried to rebuild using joins but I seem to be lost.
Can anyone help me "un-nest" this query?
SELECT
StockType2Job.Loaded
FROM
StockType2Job
WHERE
StockType2Job.IdStockType =
(SELECT StockType.IdStockType
FROM StockType
WHERE StockType.Number = '1001716.00')
AND
StockType2Job.IdStockType2JobGroup =
(SELECT StockType2JobGroup.IdStockType2JobGroup
FROM StockType2JobGroup
WHERE StockType2JobGroup.IdJob =
(SELECT Job.IdJob
FROM Job
WHERE Job.Number = '18-0085.02'
AND StockType2JobGroup.Caption = 'Breakout Room 1'))
Any help appreciated. Thanks
this query should work(on Oracle DB):
SELECT
StockType2Job.Loaded
FROM
(((StockType2Job a JOIN StockType b ON a.IdStockType=b.IdStockType)
JOIN StockType2JobGroup c ON a.IdStockType2JobGroup=c.IdStockType2JobGroup)
JOIN Job d ON c.IdJob=d.IdJob)
WHERE
b.Number = '1001716.00' AND
d.Number = '18-0085.02' AND
c.Caption = 'Breakout Room 1'
select *
from impact
where (usedin&1)=1
and impactid not in
(select impactid
from responsetime
where scontractid= scontractid )
I am learning LINQ.
I want to write this query in LINQ.
I'm told that a SQL query like this will be faster.
select *
from impact i
left outer join responsetime r on i.impactid = r.impactid
where (usedin&1)=1
and scontractid= scontractid
and r.impact is null
Converting that to linq, we get:
from i in impact
from r in responsetime.Where(rr=>i.impactid = rr.impactid).DefaultIfEmpty()
where (i.usedin&1)=1
and i.scontractid= scontractid
and r.impact is null
select i
Hi guys I have this SQL query (MSSQL), I'm doing a query where the result of joins are giving me the "top" row of newest row by date without having duplicates of results, you can find here information of what I'm doing http://goo.gl/Uv0FR The thing is this, I accomplished already the SQL query, Is Working as I'm expecting, I'm getting 1 row for each IDKEY uses in the clause "where pi.PlazaIe in ('','') without duplication
Select * from PlazaI pi
join (
Select * from PlazaE pe where
NOT EXISTS(SELECT 1 FROM PlazaE pe1
WHERE pe.Id_plaza = pe1.Id_plaza AND pe1.Fecha > pe.Fecha AND pe1.Fecha < GETDATE() and pe1.Id_Emp != 0)
) pe on pe.Id_plaza = pieepo.Id_plaza
join Emp e on pe.Id_Emp = e.Id_Emp
join View ct on ct.Id_Nodo = pe.id_nodo
where pi.PlazaIe in ('value1','value2')
The PROBLEM is when I'm trying to convert from SQL to LINQ is just can't make to happened. (I'm new in this world of Linq)
the following is my linq query.
var q1 = (from pe in db.PlazaEmpleados
where !db.PlazaEmpleados.Any
(
pe1 => (pe1.Id_plaza.Equals(pe.Id_plaza) && pe1.Fecha > pe.Fecha && pe1.Id_Emp != 0 && pe1.Fecha > DateTime.Now)
) select pe);
var q2 = (from pi in db.Context
join pe in (q1) on pi.Id_plaza equals pe.Id_plaza
select new EmpVO
{
Id_Nodo = pe.id_nodo,
Id_plaza = pi.PlazaSome,
Num_Plaza = pi.Id_plaza,
});
When I run this linq2sql query I'm getting duplicate results instead of just 1 for each value. So the thing is, I would like to know if someone can convert in a good way the SQL query to LINQ Query or point me where is the error.
thanks in advance.
Your check for the Date is different:
LINQ:
pe1.Fecha > DateTime.Now
SQL:
pe1.Fecha < GETDATE()
Isnt your LINQ supposed to be:
pe1.Fecha < DateTime.Now
I didn't find answer which resolve my problem, so what I finally did is to use the
db.ExecuteQuery<ObjectVO>(sqlQuery);
I know this is not the best practice and also don't resolve the question why my sql query and my linq query don't get the same result set, but non of the previous answer did.
The other thing is my query grown in complexity (new business logic requirement) have to join 7 table and search for Max dates and movement is some of them, so now is more complicated to transform the query to a linq to sql.
Thanks for the support.
this part:
var q1 = from pe in db.PlazaEmpleados
where !db.PlazaEmpleados.Any
(pe1 =>
pe1.Id_plaza.Equals(pe.Id_plaza) &&
pe1.Fecha > pe.Fecha &&
pe1.Id_Emp != 0 &&
pe1.Fecha < DateTime.Now
)
select pe;
In SQL you first use PlazaI then PlazaE- in Linq you both times use PlazaEmpleados.
Put your SQL query to stored procedure an add it to context. Then just call:
var q = context.MyProcedure(new object[] {"value1","value2"});
I've got a sql statement, but I can't get it working in linq. Can someone show me how I can write the following sql statement as linq?
SELECT * FROM mobileApplication
LEFT JOIN videoMobile ON mobileApplication.id = videoMobile.mobileApplicationId
AND videoMobile.videoId = 257
It's a left join with a where statement on the right table. It works in sql server 2005, but I'd like to write it in linq.
I didn't verify the syntax, but try this...
var mobileApplications = from ma in mobileApplication
join vm in videoMobile on ma.id equals vm.mobileApplicationId into j1
from j2 in j1.DefaultIfEmpty()
where vm.videoId == 257
select ma;
There is a product that will do this for you. I have found it very useful. The product name is Linqer. It is not free, but not expensive, and offers a 30 day trial. I have found very few queries it is not able to convert. It has worked well for me.
http://www.sqltolinq.com/
Its something like:
from ma in mobiledApplication.DefaultIfEmpty()
join vm in videoMobile on new { mobileApplicationId = ma.id, videoId = 257 } equals new { mobileApplicationId = vm.mobileApplicationId, videoId = vm.videoId } into videoMobileApplication
from vma in videoMobileApplication
select vma
The keys being the default if empty and using anonymous objects on the join criteria to incorporate 257 into the join.
I am pretty sure that using a where clause for the 257 will achieve the same result though...
Try some like this:
var query =
from m in mobileApplication
join v in videoMobile
on m.id = v.mobileApplicationId and v.id = 257
select m;
See here:
http://msdn.microsoft.com/en-us/library/bb397676%28v=VS.100%29.aspx
http://msdn.microsoft.com/en-us/magazine/cc163400.aspx
I'm having a problem with an sql query that i use for my mobile application which uses sql server 2005 ce. I'm not so good with t-sql, so have a problem with this query
SELECT TP.ID_TASK_MASTER, TP.ID_PROBLEM, TP.ID_TASK_PROBLE, P.DS_PROBLEM,
TP.SW_HASOK, TP.SW_HASNOK, TP.SW_HASTOK, TP.SW_HASVALUE,
TP.NO_VALUE1, TP.NO_VALUE2
FROM TASK_PROBLEMS TP
INNER JOIN PROBLEMS P
ON TP.ID_PROBLEM = P.ID_PROBLEM
GROUP BY P.DS_PROBLEM,TP.ID_TASK_MASTER, TP.ID_PROBLEM, TP.ID_TASK_PROBLE
HAVING TP.ID_TASK_MASTER = #P_IDTASKMASTER
What i try to do is doing a group by on ds_problem field, getting an error like this :
{"In aggregate and grouping expressions, the SELECT clause can contain only aggregates and grouping expressions. [ Select clause = TP,SW_HASOK ]"}
So what i made wrong? Thanks..
I suppose what you meant to do was
SELECT TP.ID_TASK_MASTER, TP.ID_PROBLEM, TP.ID_TASK_PROBLE, P.DS_PROBLEM,
TP.SW_HASOK, TP.SW_HASNOK, TP.SW_HASTOK, TP.SW_HASVALUE,
TP.NO_VALUE1, TP.NO_VALUE2
FROM TASK_PROBLEMS TP
INNER JOIN PROBLEMS P
ON TP.ID_PROBLEM = P.ID_PROBLEM
WHERE TP.ID_TASK_MASTER = #P_IDTASKMASTER
ORDER BY P.DS_PROBLEM,TP.ID_TASK_MASTER, TP.ID_PROBLEM,TP.ID_TASK_PROBLE