I want to convert this query into linq, please help:
select
mr_ssample.objectid,
mr_ssample.stcode
from mr_ssample
inner join mr_wsample on mr_ssample.objectid = mr_wsample.objectid
And mr_ssample.stcode in( select stcode
from mr_wsample)
i tried this in C#
var query = from ssamp in marineEntity.MR_SSAMPLE
join wsamp in marineEntity.MR_WSAMPLE on ssamp.OBJECTID equals wsamp.OBJECTID && ssamp.stcode.contains(wsamp.stcode)
select new
{};
However, I could not access wsamp in contains, or I dont know the alternative of this.
give this a try,
var _result = from a in mr_ssample
join b in mr_wsample on a.objectid equals b.objectid
where (from c in mr_wsample select new {c.stcode})
.Contains(new {a.stcode})
select new {a.objectid, a.stcode}
Related
I have the following query
SELECT sdb.student_due_by_month_id, sdb.due_month_year, sum(sdb.due_total_month) FROM user_student ust
INNER JOIN student std ON ust.student_id = std.student_id
INNER JOIN student_due_by_month sdb ON std.student_id = sdb.student_id
WHERE ust.user_id = 2
GROUP BY sdb.student_due_by_month_id, sdb.student_due_by_month_id, sdb.due_month_year
I need to convert it to linq or a lambda expression.
I'm trying to turn him into a linq and this is what I have so far:
var user_due_month = await (from ust in _context.Users_students
join std in _context.Students on ust.student_id equals std.student_id
join sdb in _context.Students_dues_by_months on std.student_id equals sdb.student_id
where ust.user_id == user_id
group sdb by new { sdb.student_due_by_month_id, sdb.due_month_year } into g //from here i have yet to complete the linq query, help?
I don't know what would be missing to complete my linq query correctly, and also be able to pass it to a viewmodel.
Thanks a lot!!
I'm using sql profiler to see sql generated by Ef core2.1,
this is my linq query :
var resulat = (from a in A
join b in B equals a.level=b.level
where ...
select new M1 {AId = a.id}).Distinct();
(from r in resulat
join c in C equals r.AId = c.AId
select new M2
{
CId = c.Id,
level = _helper(c.level)
}).Distinct();
Sql generated:
select t.AId,c.Id,c.level
from
(
select distinct a.id
from A a
inner join B b on a.level=b.level
where ...
) as t
inner join C c on t.AId = c.AId
What i want as result is :
select distinct c.Id,c.level
from
(
select distinct a.id
from A a
inner join B b on a.level=b.level
where ...
) as t
inner join C c on t.AId = c.AId
I have tried also using select/distinct with result IQueryable, but the sql generated is the same.
what i missed in my linq query or what i have to add to have this sql query
That's what worked for me:
Delete Distinct() from result query, this avoid adding t.AId to my selection.
Delete a helper method from one of my selection fields performe adding Distinct() to final query.
This is my query after correction:
var resulat = from a in A
join b in B equals a.level=b.level
where ...
select new M1 {AId = a.id};
(from r in resulat
join c in C equals r.AId = c.AId
select new M2
{
CId = c.Id
level = c.level
}).Distinct();
Many thanks for your comments, it really helped me.
I'm always a fan of querying the data you want directly from the table (well, DbSet) that returns the data. The process looks a bit like these steps:
I want C.Id and C.Level
That's context.Cs.
Which Cs do I want?
The ones that have a parent A, of which at least one B has the same 'level' as A and meets a couple of other criteria (the where ...).
That amounts to:
from c in context.Cs
where context.Bs.Any(b => b.level == c.A.level && <other criteria>)
select new { c.Id, c.Level }
If the where ... also contains filter criteria for A you can add predicates like && c.A == ... to the where.
Note that I assume a navigation property c.A to be present, otherwise to be created, because C has AId.
Please note below is entirely made up for example sake. I have a similar query based on an sql code but couldn't translate it to LINQ to get correct value.
The sql basically looks like this:
select * from customers c
join proucts p on c.id = p.customerid
join credit r on r.customerid=c.id and ISNULL(r.trandate, c.registeredDate) >= c.registeredDate
I also tried to tweak the above sql and put the condition inside where and it also returns the same value I am getting in my #2 LINQ below(which is incorrect).
How can I use c (customer) inside .Where of credit? see code
1.
from c in customers
join p in products on c.id = p.customerid
join cr in credit.Where(r=> r.tranDate => c.registeredDate!=null?c.registeredDate : r.purchaseDate) on c.id=cr.customerid
...
2.
I know you would suggest why not just put it in a where below like below but I am getting incorrect value.
from c in customers
join p in products on c.id = p.customerid
join cr in credit on c.id=cr.customerid
where r.tranDate => c.registeredDate!=null?c.registeredDate : r.purchaseDate
Is there a workaround? I have tried tons of others but won't get me the correct one.
LINQ supports only equijoins. Any additional criteria should go to where clause. And yes, the other range variables are inaccessible from the join inner sequence, so the filtering should happen before or after the join.
So this SQL query:
select * from customers c
join products p on c.id = p.customerid
join credit r on r.customerid = c.id
and ISNULL(r.trandate, c.registeredDate) >= c.registeredDate
directly translates to this LINQ query:
from c in customers
join p in products on c.id equals p.customerid
join cr in credit on c.id equals cr.customerid
where (cr.tranDate ?? c.registeredDate) >= c.registeredDate
select new { c, p, cr };
Optionally, the condition
(cr.tranDate ?? c.registeredDate) >= c.registeredDate
can be replaced with
(cr.tranDate == null || cr.tranDate >= c.registeredDate)
(from SN1 in uow.SystemNotifications
join sn2 in
(
from SN2 in uow.SystemNotifications
select new { SN2.UserId, SN2.NotificationTypeId, SN2.ItemId }
).Distinct()
on new { SN1.UserId, SN1.NotificationTypeId }
equals new { sn2.UserId, sn2.NotificationTypeId }
select SN1).ToList();
When I execute this query, Distinct() is not working. It selects all the records in the result of inner query. How to modify this to get distinct rows in the inner query result.
You can use group by for this, Folowing code helps you to solve this problem,
var rootcategories2 = (from p in sr.products
group p.subcategory by p.category into subcats
select subcats);
let me know if u have any problem.
I am struggling with how to write the below equivalent as LINQ. Truly I guess I am only struggling with how I represent the INNER JOIN () portion. Is that called a Nested Join? Anonymous Join? I am not even sure. Anyway, big thanks to anyone who can point me true. Even if it is just what this is called so I can BING it properly.
SELECT p.PersonID, p.FirstName, p.MiddleName, p.LastName, cp.EnrollmentID, cp.EnrollmentDate, cp.DisenrollmentDate
FROM vwPersonInfo AS p
INNER JOIN (
SELECT c.ClientID, c.EnrollmentID, c.EnrollmentDate, c.DisenrollmentDate
FROM tblCMOEnrollment AS c
LEFT OUTER JOIN tblWorkerHistory AS wh
ON c.EnrollmentID = wh.EnrollmentID
INNER JOIN tblStaffExtended AS se
ON wh.Worker = se.StaffID
WHERE (wh.EndDate IS NULL OR wh.EndDate >= getdate())
AND wh.Worker = --WorkerID Param Here
) AS cp
ON p.PersonID = cp.ClientID
ORDER BY p.PersonID
just put the inner query in its own variable. (It will be translated into one single SQL expression)
var innerQuery = from x in db.tblCMOEnrollment
where ...
select ...;
var query = from a in vwPersonInfo
join b innerQuery on p.PersonID equals cp.ClientID
select ...;
I think you can do this by writing a second method and joining on that method:
private static IEnumerable<Table> GetData(int joinKey)
{
return (from x in context.TableB.Where(id => id.Key == joinKey select x).AsQueryable();
}
Then you can do your normal query:
var query = from c in context.TableA
join GetData(c.PrimaryKeyValue)