Convert SQL to LINQ Query [closed] - c#

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

Related

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.

Converting sql to lambda expression or Linq [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
Hi I have following sql statement I want to convert it to lambda expression or linq
select
a.lid
,a.name
,a.notes
,lrh.e
,lrh.date
from rate a
left outer join lab_history lrh on(lrh.lab_id=a.lid)
Please let me know how to change this to lambda expression or linq. Thanks
You can use this query:
from a in db.rate
join lrh db.lab_history on a.lid equals lrh.lab_id
into lrhs
from lrh in lrhs.DefaultIfEmpty()
select new
{
lid = a.lid,
... another props
}
You can try
var labRateObj = (from a in ContextObj.rate
join lrh in ContextObj.lab_history on a.lid equals lrh.lab_id into labrate
from s in labrate.DefaultIfEmpty()
select new {
lid = a.lid,
name = a.name,
notes = a.notes,
e = lrh.e,
date = lrh.date
});

convert sql query into lambda expression [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Here is my sql query i want to convert it into lambda expression
Select P.PlaceName As UnitNumber,
PB.PlaceName,
A.Locality,
A.SubLocality,
A.Sublocality_Level_1
from Listing L
inner join Place P ON L.PlaceId = P.Id
Inner Join Place PB ON PB.Id = P.ParentPlaceId
Inner Join [Address] A ON A.Id = PB.AddressId
Where L.Id =9
Thanks in advance .
Query expression is much simpler in this case. I don't recommend you to use lambdas with many joins
from l in db.Listing
join p in db.Place on l.PlaceId equals p.Id
join pb in db.Place on p.ParentPlaceId equals pb.Id
join a in db.Address on pb.AddressId equals a.Id
where l.Id == 9
select new {
UnitNumber = p.PlaceName,
pb.PlaceName,
a.Locality,
a.SubLocality,
a.Sublocality_Level_1
}
db is your context

Categories