translate t-sql in linq - linq

How can I write the below t-sql in Linq?
select (select COUNT(col1) FROM t2 WHERE col2 = t1.col2 and col1 = t1.col1) as total,
t1.col1,t1.col2...................
from t1

var res = from t1 in context.t1s
select new
{
total = context.t2s.Where(t2=> t2.col1 == t1.col1 && t2.col2 == t1.col2).Count(),
t1.col1,
t1.col2,
};
Edit:
And if you have your FK-relations set up correctly in the database and your DBML you can use the simpler version below
var res = from t1 in context.t1s
select new
{
total = t1.t2s.Count(),
t1.col1,
t1.col2,
};

Try using LinqPad. It is great for translating between SQL and LINQ queries.
http://www.linqpad.net/

Related

Oracle Hierarchical queries: Translate START WITH ... CONNECT BY PRIOR into 'Recursive Subquery Factoring'

How would the following START WITH / CONNECT BY hierarchical query look like when translated into a RECURSIVE SUBQUERY FACTORING hierarchical query with WITH clause:
SELECT t1.id
FROM table1 t1, table2 t2
WHERE t1.version_id = t2.id
AND t1.baseline_date = TRIM (TO_DATE ('2015-05-26', 'yyyy-mm-dd'))
AND t2.entry_date = t1.baseline_date
START WITH t1.id IN (SELECT id
FROM table1
WHERE parent_id = 101015)
CONNECT BY PRIOR t1.id = t1.parent_id
ORDER SIBLINGS BY t1.child_index;
I think you want:
WITH rsqfc (id, child_index, baseline_date) AS (
SELECT t1.id,
t1.child_index,
t1.baseline_date
FROM table1 t1
INNER JOIN table2 t2
ON ( t1.version_id = t2.id
AND t2.entry_date = t1.baseline_date )
WHERE t1.parent_id = 101015
UNION ALL
SELECT t1.id,
t1.child_index,
t1.baseline_date
FROM rsqfc r
INNER JOIN table1 t1
ON (r.id = t1.parent_id)
INNER JOIN table2 t2
ON ( t1.version_id = t2.id
AND t2.entry_date = t1.baseline_date )
)
SEARCH DEPTH FIRST BY child_index SET order_id
SELECT id
FROM rsqfc
WHERE baseline_date = DATE '2015-05-26';
However, without sample data it is difficult to be sure.

How do you write an INNER JOIN with an "OR" in Linq

I am trying to write a Linq query to generate the following SQL
SELECT
[t1].[Id], [t2].[value3]
FROM
[Table1] AS [t1]
INNER JOIN
[Table2] AS [t2] ON [t1].[Id] = [t2].[value1]
OR [t1].[Id] = [t2].[value2]
I have seen lots of examples for how to do multiple joins, but none for how to do this type of "one or the other" join.
var result = from t1 in context.Table1
from t2 in context.Table2
where (t1.Id == t2.value1 || t1.Id == t2.value2)
select new
{
t1.Id,
t2.value3
};
INNER JOIN
var query =
from t1 in context.Table1
from t2 in context.Table2.Where(t2 => t1.Id == t2.value1 || t1.Id == t2.value2)
select new
{
t1.Id,
t2.value3
};
LEFT JOIN
var query =
from t1 in context.Table1
from t2 in context.Table2.Where(t2 => t1.Id == t2.value1 || t1.Id == t2.value2)
.DefaultIfEmpty()
select new
{
t1.Id,
t2.value3
};

Want to change SQL query to LINQ

I want to change the below simple SQL query into LINQ , how do I change it ?
select * from table1 where isPaid = 'true' and Id in (select Id from table2 where EmployeeId = 12)
similar to this ?
from pa in db.PaymentAdvices
where pa.IsPaid == true
orderby pa.PaidDate descending
select pa;
Here code linq to sql:
from t1 in table1
join t2 in table2 on t1.Id equals t2.Id
where t2.EmployeeId = 12
select t1
Hope usefull!
if the field isPaid has datatype is Boolean:
from t1 in table1
join t2 in table2 on t1.Id equals t2.Id
where t2.EmployeeId = 12 and t1.isPaid == true
select t1
if the field isPaid has datatype is String:
from t1 in table1
join t2 in table2 on t1.Id equals t2.Id
where t2.EmployeeId = 12 and t1.isPaid.Equals("true")
select t1

Join and batch update in LINQ to SQL

Can something like this be written in LINQ to SQL:
UPDATE Table1
SET CustomerName = t2.Name
FROM Table1 t1 JOIN Table2 t2
ON t1.TableID = t2.TableID
var result = from t1 in context.Table1
join t2 in context.Table2 on t1.TableID = t2.TableID
select new {t1, t2};
result.ToList().ForEach(i => i.t1.CustomerName = i.t2.Name);
context.SubmitChanges();

LINQ Join Where Clause

I'm struggling with a join/where clause with what is a rather simple sql select statement.
I am trying to retrieve a list of product information from tb1 with the where condition behind situated in tbl2 but this must be joined by three different columns.
so the SQL would look something along the lines of:
SELECT tb1.*
FROM tb2 INNER JOIN
tb1 ON tb2.Col1 = tb1. Col1 AND tb2.Col2 = tb1. Col2 AND
tb2.Col3 = tb1.Col3
WHERE (tb2.Col1 = col1) AND (tb2.Col2 = col2) AND (tb2.Col4 = string)
ColX is the main where clause with the string to be passed in as parameter; all other columns are within the contexts.
How do you implement multiple joins with a where clause?
And shoves in the right direction, muchly appreciated.
To join on multiple field in LINQ, you have to create a new anonymous type containing the columns you want to compare and then use that anonymous type in the join:
var results = from t1 in context.tb1
join t2 in context.tb2
on new { t1.Col1, t1.Col2, t1.Col3 } equals
new { t2.Col1, t2.Col2, t2.Col3 }
where t2.Col1 == col1 && t2.Col2 == col2 && t2.Col4 == someString
select t1;
And here is the equivalent Lambda Syntax:
var results = context.tb1.Join(
context.tb2,
t1 => new { t1.Col1, t1.Col2, t1.Col3 },
t2 => new { t2.Col1, t2.Col2, t2.Col3 },
(t1, t2) => new { t1, t2 })
.Where(o => o.t2.Col1 == col1
&& o.t2.Col2 == col2
&& o.t2.Col4 == someString)
.Select(o => o.t1);
As you can see, in the case of joins, query syntax usually produces an easier to read statement.
You can also include the WHERE clause in lamda syntax in the reference to the table you're joining on.
var query = from pt in dc.ProjectTasks
join ttab in dc.TimeTaskAssigns on pt.Id equals ttab.ProjectTaskId
join ttb2 in dc.CMS_TAT_TIMEs.Where(a => a.WIP_STATUS == 'B') on ttab.CmsTimeUno equals ttb2.TIME_UNO
select pt;
Seems obvious now, doesn't it? It took me a long time to find that solution.
Also you can group result and use subquery
var innerGroupJoinQuery2 =
from category in categories
join prod in products on category.ID equals prod.CategoryID into prodGroup
from prod2 in prodGroup
where prod2.UnitPrice > 2.50M
select prod2;
ref.: join clause (C# Reference)

Resources