Use this tag for questions related to the conversion of SQL statements into LINQ statements.
Questions tagged [sql-to-linq-conversion]
243 questions
1
vote
0 answers
Can not convert SQL into Linq with Group By
I am trying to convert this sql into linq but so far not able to
Below code is successfully giving result as I need
SELECT [dbo].[Ingredient].[IngredientId]
,[EnglishName]
,[HindiName]
,COUNT([dbo].[RecipeIngredient].RecipeId) As…

Vishal Bakhtani
- 11
- 1
1
vote
3 answers
SQL to LINQ with sub joins and sub queries
I am fairly new to LINQ and having issues when converting this SQL to LINQ. Can any one help me achieve this?
Select DISTINCT u.ID from
(Select x.ID from table1 x where x.Info = 0) u
where u.ID not in
(select c.ID from table1 c where c.Info =…

user1098924
- 79
- 7
1
vote
1 answer
how to select value from group by using linq to sql
Can anyone help me to write the equivalent sql to linq in C#
select
COUNT(ot.UserId) as orderCount, SUM(ot.TotalAmount) as TotalAmount,
MAX(u.UserName) as UserName, MAX(c.name) as ClientName
from OrderTemplate ot
join User u
on u.userid =…

Robin Halder
- 253
- 2
- 14
1
vote
3 answers
Convert paging logic from SQL to Linq
I am totally new to Linq.
I need to convert paging logic from SQL to Linq.
My paging logic in SQL is:
where num BETWEEN ((@pageNumber-1)*@pageSize) + 1 and (@pageSize * @pageNumber)
So, if pageNumber is 2 and pageSize is 30, my query will pull…

Purnima Naik
- 2,393
- 5
- 17
- 25
1
vote
1 answer
Converting Sql to Linq (to Entities)
I have a query like that and it works normally. If a student gets more than one course, it list all of them:
SELECT ks.KullaniciKodu as username, ks.Sifre as password, k.adi as firstname, k.soyadi as lastname, k.Email as email,
MAX(CASE WHEN…

LacOniC
- 824
- 12
- 21
1
vote
2 answers
Linq query for left-join
I have 3 tables:
* UserSettingsType { general, UserSpecific)
* UserSettingsOption { Currency:general, Language:general, Location:Userspecific }
* UserSettingsValue { Currency:USD, Location:US }
If I run the SQL query:
select ust.Name Type, uso.Name…

sppc42
- 2,994
- 2
- 31
- 49
1
vote
1 answer
how to change sql query to linq?
How to change this sql query to linq? I have three join that last join is select query.
select mod.ModSerial,
met.MetSerial,
met.WaterNo,
met.PowerNo,
Gro.GroupDate
from MetlDB.dbo.tblMet AS met
join…

mahdis dezfouli
- 173
- 3
- 19
1
vote
1 answer
Left Join 2 tables with main table using LINQ
This is my Query in SQL :
Select distinct * from tr.Table1
Left Outer join tr.Table2 on tr.Table1.ID = tr.Table2.ID
Left Outer join tr.Table3 on tr.Table2.AId= tr.Table3.ID
where tr.Table1.Deleted =1 and tr.Table1.Ready=1 and tr.Table1.Show=0
The…

Dhrumil
- 3,221
- 6
- 21
- 34
1
vote
0 answers
Need help converting sql query with pivot to LINQ
Can someone help me convert the following to LINQ? as I am new to C# and link
SELECT
DayWise, [1] AS Lead1, [2] AS Lead2, [3] AS Lead3,
[4] AS Lead4, [5] AS Lead5, [19] AS Greater
FROM (
SELECT CONVERT(VARCHAR(10), dt_createdon, 101)…

user1939396
- 11
- 2
1
vote
2 answers
Rewriting a SQL with LINQ
I have this SQL:
select * from ADDRESSES where ZIPCODE ='66210'and ADDRESS_K in
(select ADDRESS_K from GROUPADDRESS where GROUP_K in
(select GROUP_K from GROUPS where NPI = 'groupnpi' and TAXID = 'grouptin')
and…

ConfusedSleepyDeveloper
- 823
- 4
- 12
- 22
1
vote
1 answer
Convert a SQL query to LINQ query
I have the following SQL query
SELECT *
FROM LOC l
JOIN CLA c ON l.IdLoc = c.IdLoc
JOIN FA f on c.IdCla = f.IdCla
LEFT JOIN CON co ON f.IdCla = co.IdCla
AND co.DeletedDate IS NULL
AND co.IdUser = f.IdUser
WHERE f.IdUser = 7
AND…

AlexB
- 7,302
- 12
- 56
- 74
1
vote
1 answer
LINQ to SQL: Left join a table with itself and compute an average
I'm trying to write the following query in LINQ to SQL. The table contains a list of sessions arranged by users, and the query computes the average amount of time between consecutive sessions for each user. It uses a left join, so that users who…

Andrew Mao
- 35,740
- 23
- 143
- 224
1
vote
1 answer
Left outer join and multiple counts SQL to LINQ
How would this query using an inner join, left outer join, group by and two counts be converted to linq?
SELECT
c.EndowmentID,
COUNT(DISTINCT f.CriterionID) AS RequiredCriteria,
COUNT(r.ChoiceID) AS Response
FROM
Criteria c
INNER…

Talley
- 15
- 4
1
vote
2 answers
Need help to convert T-Sql into Linq
I have never used before Linq, and really have lack of time to study. My little knowledge is not enough to do this and I need your help.
Here is a code which I need to convert into Linq. (I am using EF6 and context)
WITH messages AS (
SELECT…

ADO_kg
- 65
- 2
- 13
1
vote
1 answer
Entity Framework - complex query - and CSV export
A relatively simple task... I need to pull data from Database and export it to CSV file. The problem is that the data is queried from several joined tables and I'm not sure what is the best way to get data using EF and serialize it.
My SQL query…

lekso
- 1,731
- 3
- 24
- 46