The Translation of an SQL query to LinQ - sql

Due to wierd request from a Customer, I have managed to figure out how to implement it
using SQL query, but I couldn't translate it into LinQ.
SELECT (SELECT count(*) FROM table1 where attribute1 like 'value1'),
(SELECT count(*) FROM table2 where attribute2 like 'value2')
What the translation of the query to LinQ?

You could just supply a predicate to the Count() function
var result = new {
Count1 = table1.Count(r => r.attribute1.Contains("value1")),
Count2 = table2.Count(r => r.attribute2.Contains("value2"))
};

var count1 = (from i in table1 where SqlMethods.Like(i.attribute1, "value1") select i).Count();
var count2 = (from i in table2 where SqlMethods.Like(i.attribute2, "value2") select i).Count();

Related

how to convert postgres query looking for records where value appears only once to knex

I am trying to convert a postgres valid query that returns what I require to a knex query...
I keep getting various errors and can't write the knex query properly.
I have several versions of the query in postgres to find records in a table that appear only once and that have a specific user_id.
here are the different variants:
select * from project_authors t1
where (select count(*) from project_authors t2
where t1.project_id = t2.project_id) = 1
AND t1.user_id=2
OR
select * from project_authors t1
where NOT exists
(select 1 from project_authors t2
where t1.project_id = t2.project_id and t1.user_id <> t2.user_id) AND t1.user_id=2
OR
select * from project_authors t1
INNER JOIN (SELECT t3.project_id FROM project_authors t3 GROUP BY t3.project_id
HAVING COUNT (t3.project_id)=1) t2
ON t1.project_id=t2.project_id Where t1.user_id=2
the way I write the knex query is using parameters supplied in the request (for example):
const _readDbSingleAuthor = (table1, column1, criteria) => {
return db(`${table1} as t1`)
.select("*")
.where(
db(`${table1} as t2`)
.select("*")
.count("*")
.where(`t1.${column1}`, "=", `t2.${column1}`),
"=",
1
)
.andWhere(criteria);
I would appreciate any help...
the version above gave me a "maximum call stack exceeded" - so I hit the stackoverflow literally.
a version using join was the closest thing but gave me a result saying that the project_id column was ambiguous:
const _readDbSingleAuthor = (table1, data, column1, criteria) => {
return db
.select(data)
.from(`${table1} as t1`)
.join(
db
.select(column1)
.from(`${table1} as t3`)
.count(`${column1} as count`)
.groupBy(column1)
.having("t3.count", "=", 1)
.as("t2"),
`t2.${column1}`,
"=",
`t1.${column1}`
)
.where(criteria);
};

How can i write Linq of this query ? ( Linq C#)

Sql Query is :
select T1.SellerId , count(T2.productId)
from
tbl_1 T1
left join
tbl_2 T2
on T1.SellerId = T2.sellerId
where VisitorUserId = 'bf8581b04429fdf56c6ebc'
group by T1.SellerId
I Need This SQL Query in Linq C#.
Assuming that VisitorUserId is a column of tbl_1 and assuming you use ORM like Entity Framework with navigation properties for foreign keys relationships it should be smth like this:
context.tbl_1
.Where(i => i.VisitorUserId = 'bf8581b04429fdf56c6ebc')
.Select(i => new { SellerId = i.SellerId, ProductCount = i.tbl_2.Count() })

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.

Derived tables in Linq to SQL

I would like to implement this in Linq to SQL:
select * from (
select * from Orders) as A
Obviously this is a pointless example, but I just can't figure out how to do it!
Try something like this:
var subquery = from row in Orders select row;
var query = from row in subquery select row;
using (var dc = new NorthwindDataContext())
{
var A = from ordOuter in
from ordInner in dc.Orders
select ordInner
select ordOuter;
}

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)};