convert sql query to linq - sql

I have this SQL query :
select RoleID
from tblUserRole
where UserID = 1
and RoleID in
(Select ID from tblGreenRole
Where
IsFullAccess = 1)
How can I convert this query to linq?

Something like this:
var result = (from t1 in context.tblUserRole
join t2 in context.tblGreenRole on t1.RoleID equals t2.ID
where t1.UserID == 1 && t2.IsFullAccess == 1).ToList();
If IsFullAccess is of type bit then change to && t2.IsFullAccess == true or just && t2.IsFullAccess

Related

SQL QUERY UPDATE CONDITION

I need to add one condition to this query :
UPDATE o36t_orders s
SET s.bonifico = EXISTS (SELECT 1
FROM mytable d
WHERE d.Descrizione_operazione
LIKE CONCAT('%', s.shipping_number,'%') )
The condition should be to do the update only if s.bonifico != 1
What you probably want is this:
UPDATE o36t_orders s
SET s.bonifico = EXISTS (SELECT 1
FROM mytable d
WHERE d.Descrizione_operazione
LIKE CONCAT('%', s.shipping_number,'%') )
WHERE s.bonifico != 1;
You can use this code for your problem:
//CONNECT DB - FETCH RESULT
$variable=$row['s.bonifico'];
if($variable!='1'){
UPDATE o36t_orders s SET s.bonifico = EXISTS (SELECT 1 FROM mytable d WHERE d.Descrizione_operazione LIKE CONCAT('%', s.shipping_number,'%') )
}

SQL Server Outer Apply Query Optimization

I have two tables - Table A and Table B.
Table A has 121,903 rows. Table B has only 95 rows.
I need to join Table A with Table B such that I will get first row of Table B which have matching rows with Table A order by sort criteria.
I am using the following query to get the results. It is returning results correctly but has performance issues.
;WITH [TableAB] AS
(
SELECT * FROM #TableA A
OUTER APPLY
(
SELECT TOP 1 * FROM #TableB
WHERE
([Col1] = A.[Col1]OR [Col1]IS NULL)
AND ([Col2] = A.[Col2]OR [Col2]IS NULL)
AND ([Col3] = A.[Col3]OR [Col3]IS NULL)
AND ([Col4] = A.[Col4]OR [Col4]IS NULL)
AND ([Col5] = A.[Col5] OR [Col5] IS NULL)
AND ([Col6] = A.[Col6]OR [Col6]IS NULL)
AND ([Col7] = A.[Col7]OR [Col7]IS NULL)
AND ([Col8] = A.[Col8]OR [Col8]IS NULL)
AND ([Col9] IS NULL)
AND ([Col10] IS NULL)
AND ([Col11] = A.[Col11] OR [Col11] IS NULL)
AND ([Col12] = A.[Col12]OR [Col12] IS NULL)
AND ([Col13] = A.[Col13]OR [Col13]IS NULL)
AND ([Col14] = A.[Col14] OR [Col14] IS NULL)
AND ([Col15]= A.[Col15]OR [Col15]IS NULL)
AND ([Col16] = A.[Col16] OR [Col16] IS NULL)
AND ([Col17]= A.[Col17]OR [Col17]IS NULL)
AND ([Col18]= A.[Col18]OR [Col18]IS NULL)
AND ([Col19]= A.[Col19]OR [Col19]IS NULL)
AND ([Col20] = A.[Col20] OR [Col20]IS NULL)
ORDER BY [SortCriteria]
) B
)
SELECT * FROM [TableAB]
It currently takes ~1 minute to execute this query. Is there any way I can rewrite the query to improve the performance?
Note that it is a data warehouse system, the above query is part of a large query which uses CTE table "TableAB".
Thanks.
Since the bulk of the execution is spent sorting TableB, the most likely candidate for improving performance would be to add an index that covers SortCriteria and INCLUDES all the columns in TableB that are selected in the query.

Converting SQL to LINQ (left join on two fields)

I need help to convert this expression to LINQ.
In this example:
TableA[IDTABLE_A, NAME]
TableA[IDTABLE_B, IDTABLE_A, REL]
SELECT *
FROM TableA a
LEFT JOIN TableB b
ON a.IDTABLE_A = b.IDTABLE_A
AND b.IDTABLE_B = 3
Thanks in advance.
Try this:-
var query = from a in data1
join b in data2.Where(x => x.BID == 3)
on a.AID equals b.AID into ab
from c in ab.DefaultIfEmpty()
select new
{
AID = a.AID,
AName = a.AName,
BName = c == null ? "No Records" : c.BName
};
Complete Working Fiddle Here.

Linq Left Join returns repeated same rows for all set

my linq returns all repeated same rows for all set
run SQL in DB :
select top 20 * from t1
left join t2
on t1.sid= t2.sid
and t1.pid=t2.pid
where(t2.sid is null and t1.pid='r')
i can get 20 different rows of result.
then i write Linq:
Entities dbconn = new Entities();
List<t1> myResult = (
from t1Data in dbconn.t1
join t2Data in dbconn.t2
on new { sid = (int)t1.sid, pid= t1.pid}
equals new { sid= (int)t2.sid, pid= t2.pid}
into joinSet
from joinUnit in joinSet.DefaultIfEmpty()
where (joinUnit == null) && (t1.pid== "r")
select t1Data
).Take(20).ToList();
all rows of result are the some row.
select t1Data is wrong as t1Data is from the original dataset.
Instead, select the joined result: select joinSet.

Linq to SQL equivalent of SUM GROUP BY SQL statement

I'm having a hard time figuring out how to translate this simple SQL statement to (c#) linq to SQL :
SELECT table1.vat, SUM(table1.QTY * table2.FLG01 + table1.QTY * table2.FLG04)
FROM table1
inner join table2 on table2.key= table1.key
where '2010-02-01' <= table1.trndate and table1.trndate <= '2010-02-28'
Group by table1.vat
Any help is appreciated
I'm still learning LINQ but this seems to work
var result = from t1 in table1
from t2 in table2
where t1.key == t2.key && DateTime.Parse("2010-02-01") <= t1.trndate && t1.trndate <= DateTime.Parse("2010-02-28")
group new {t1,t2} by t1.vat into g
select new { vat = g.Key, sum = g.Sum(p => p.t1.QTY*p.t2.FLG01 + p.t1.QTY*p.t2.FLG04)};
I hope in translates well to LINQ to SQL because I only tried it on objects.
So with help of Jonas the above query reads this (using inner join):
var result = from t1 in table1
join t2 in table2 on t1.key equals t2.key
where DateTime.Parse("2010-02-01") <= t1.trndate && t1.trndate <= DateTime.Parse("2010-02-28")
group new {t1,t2} by t1.vat into g
select new { vat = g.Key, sum = g.Sum(p => p.t1.QTY*p.t2.FLG01 + p.t1.QTY*p.t2.FLG04)};