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