How to convert this SQL to LINQ of ASP.NET MVC? [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have this SQL statement:
select
table1.DIRECTORATE_ID,
table2.PLA_NAME + '//' + table1.ENGLISH_DIRECTORATE_CODE as EngCode
from table1
inner join table2 on table1.PLA_ID = table2.PLA_ID order by EngCode
I need to do the same type of query in my EF code that gives me a List<>. Something like this:
var query = (from t1 table1.DIRECTORATE_ID, table2.PLA_NAME + '//' + table1.ENGLISH_DIRECTORATE_CODE as EngCode
from table1
inner join table2 on table1.PLA_ID = table2.PLA_ID order by EngCode)
.ToList();

You've got column references in your from clause. In SQL, the order is (in this particular example)
select
from
join
order by
In LINQ, the order is
from
join
orderby
select
You can write this in Query Syntax
var query = from t1 in table1
join t2 in table2 on t1.PLA_ID equals t2.PLA_ID
let EngCode = t2.PLA_NAME + "//" + t1.ENGLISH_DIRECTORATE_CODE
orderby EngCode
select new
{
t1.DIRECTORATE_ID,
EngCode
}
var data = query.ToList();
Or with extension methods
var data = table1
.Join(
table2,
t1 => t1.PLA_ID,
t2 => t2.PLA_ID,
(t1, t2) => new
{
t1.DIRECTORATE_ID,
EngCode = t2.PLA_NAME + "//" + t1.ENGLISH_DIRECTORATE_CODE
})
.OrderBy(x => x.EngCode)
.ToList();

Related

Convert Sql Statement to Entity Framework Core [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How can i convert this sql query to linq in entity framework core ?
Select a.Id,a.Url,c.Title,a.ParentId,b.totalSubCats
From [Tbl_Menu] As a
LEFT OUTER JOIN
( Select ParentId, Count(*) As totalSubCats From [Tbl_Menu] Group By ParentId ) As b
On a.Id=b.ParentId
LEFT OUTER JOIN [Tbl_Menu] As c On a.Id = c.Id
ORDER BY a.Id
And we have this columns in Menu_Tbl
Id , Url , Title , Description , ParentId
For Left Join I wrote this query but can`t run
var query = (from m1 in _context.Menu_Tbl
join m2 in _context.Menu_Tbl on m1.Id equals m2.Id
into m3 from m in m3.DefaultIfEmpty() select m1);
I think it would be easier to just make that query a stored procedure and then call the stored procedure using yourReturnedDbSet objectReceived = context.yourReturnedDbSet.FromSqlRaw("EXECUTE StoredProcedureName #exampleParam"). But as for converting it you can pretty much use the same syntax if you have the correct objects. You will also need a list that holds the type you are being returned.
This one should work. Just divide big query by small pieces (subqueries)
var grouped =
from m in ctx.Tbl_Menu
group m by new { m.ParentId } into g
select new
{
g.Key.ParentId,
totalSubCats = g.Count()
};
var query =
from a in ctx.Tbl_Menu
join b in grouped on a.Id equals b.ParentId into gj
from b in gj.DefaultIfEmpty()
join c in ctx.Tbl_Menu on a.Id equals c.Id into ggj
from c in ggj.DefaultIfEmpty()
select new
{
a.Id,
a.Url,
c.Title,
a.ParentId,
b.totalSubCats
};

retrieving values from one table based condition on the retrieving table and reefrencing table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have two tables tblorders and tblprogramme as shown in below images.
I will select PartyName (from tblorders) from a dropdownlist in asp.net, based on that only those JobNo should come whose all the Status (from tblprogramme) is dispatched.
I want query for that.
e.g
I want "Parry's"(in table tblorders) That "JobNo" whose all the "Status"(in tblprogramme ) is "Dispatched")
In my case from below table images
Query should return "JobNo"( 2).as Parry's this JobNo's (in tblProgramme") has status (Dispatched ib tblprogramme
Here is the image).
SELECT t1.JobNo
FROM tblOrders t1
WHERE t1.PartyName = 'Parry'
AND NOT EXISTS ( SELECT *
FROM tblProgramme t2
WHERE t1.JobNo = t2.JobNo
AND t2.ProgrammeStatus <> 'Dispatched' );
Try below,
SELECT *
FROM (
SELECT
jobno,
name,
TotalCount = (
SELECT COUNT(*)
FROM tblProgramme
WHERE jobno = o.jobno
),
DispatchedCount = (
SELECT COUNT(*)
FROM tblProgramme
WHERE jobno = o.jobno AND status = 'dispatched'
)
FROM tblOrder o
) t
WHERE name = 'Parry'
AND TotalCount = DispatchedCount

INSERT, subquery, VALUES [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This query insert into table values from othes table(it returned more than 1 rows)
INSERT INTO metric_values(mv_db_id, mv_cat_id)
(SELECT DISTINCT dbs_id, cat_id FROM
(SELECT DISTINCT cat_id, dbs_id FROM categories
INNER JOIN users ON (cat_id = us_category_id)
INNER JOIN hits ON (us_id = h_user_id)
INNER JOIN dbs ON (h_db_id = dbs_id)
WHERE h_datetime = '2009-09-28'
GROUP BY cat_id, dbs_id)foo)
But I have some more variables(mv_metric_id, mv_period_id, mv_period_startdate), which values I receive from program (c#). Anybody knows how to insert this variables with this query?
Something like this:
INSERT INTO metric_values(mv_metric_id, mv_period_id, mv_period_startdate, mv_db_id, mv_cat_id)
VALUES (1, 1, '2009-09-28', (SELECT DISTINCT dbs_id, cat_id
FROM (SELECT DISTINCT cat_id, dbs_id
FROM categories
INNER JOIN users ON (cat_id = us_category_id)
INNER JOIN hits ON (us_id = h_user_id)
INNER JOIN dbs ON (h_db_id = dbs_id)
WHERE h_datetime = '2009-09-28'
GROUP BY cat_id, dbs_id)foo))
You can put your own values in the outermost SELECT clause, for example:
INSERT INTO metric_values(mv_metric_id, mv_period_id, mv_period_startdate, mv_db_id, mv_cat_id)
(SELECT DISTINCT MYDATA1, MYDATA2, MYDATA3, dbs_id, cat_id FROM
(SELECT DISTINCT cat_id, dbs_id FROM categories
INNER JOIN users ON (cat_id = us_category_id)
INNER JOIN hits ON (us_id = h_user_id)
INNER JOIN dbs ON (h_db_id = dbs_id)
WHERE h_datetime = '2009-09-28'
GROUP BY cat_id, dbs_id)foo)
Of course this will make the whole row DISTINCT so might have regroup your SELECT clauses.

Linq & ASP.NET MVC: how to use "or" and "equals" in join statement [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
How to translate this sql query in a linq query, please?
USE [BiblioDB]
GO
SELECT *
FROM Exemplaire e
LEFT JOIN location l
ON e.ExemplaireId = l.ExemplaireId
INNER JOIN Retour r
ON l.LocationId is null or l.LocationId = r.LocationId
Go
I tried this linq query but the fourth line doesn't work.
BiblioDBContext biblioDBContext = new BiblioDBContext();
var query = from e in biblioDBContext.Exemplaires
join l in biblioDBContext.Locations on e.ExemplaireId equals l.LocationId
join r in biblioDBContext.Retours on l.LocationId == null || l.LocationId equals r.LocationId r.LocationId
select e;
Try this:
BiblioDBContext biblioDBContext = new BiblioDBContext();
var query = from e in biblioDBContext.Exemplaires
join l in biblioDBContext.Locations on e.ExemplaireId equals l.ExemplaireId into el
from l in el.DefaultIfEmpty()
from r in biblioDBContext.Retours
where (l.LocationId == null) || (l.LocationId.Equals(r.LocationId))
select new {e, l, r};
The issue is the missing DefaultIfEmpty() clause that is needed for Left Joins.

Convert SQL to LINQ Query [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
I have the following SQL query and I need to have it in LINQ, I tried several things but I can not get it working.
Here is the SQL query
SELECT ST.Description, ST.STId, COUNT(SI.SIId) AS Expr1
FROM BP INNER JOIN
MbrBP ON BP.BPId = MbrBP.BPId INNER JOIN
SI ON BP.BPId = SI.BPId RIGHT OUTER JOIN
ST ON SI.STId = ST.STId
WHERE (BP.CountryId = 1) AND (BP.RegionId = 1) AND (MbrBP.MemberId = 1)
AND (SI.IsActive = 1)
GROUP BY ST.Description, ST.STId
UNION
SELECT ST.Description, ST.STId, COUNT(SI.STId) AS Expr1
FROM SI RIGHT OUTER JOIN
ST ON SI.STId = ST.STId
GROUP BY ST.Description, ST.STId

Categories