I have looked at many similar question on SO but seems its not that straight forward.
The problem is, most of them are dealing with IEnumerable
where in my case I have two IQueryable
dbset
s.
The situation is somewhat similar to the question here.
Student
id Name
1 a1
2 b1
3 c1
Images
id Image StudentId Status ModifiedOn
1 1.jpg 1 Active 2021-03-12 02:02:32.580
2 2.jpg 1 Deleted 2021-03-12 02:01:32.580
3 3.jpg 2 Deleted 2021-03-12 02:02:32.580
4 4.jpg 2 Deleted 2021-03-12 02:01:32.580
Result
should be
id Name Image
1 a1 1.jpg
2 b1 3.jpg
3 c1 NULL
I can do this with TSQL and nested WITH qqueries, where one selects Status = Active, and the other selects Status != Active, then merge these two and select the TOP 1.
But since the requirement is to write the equivalent LINQ, I started with the below query, since I don't know a good way to do a merge of CASE WHEN on Status = Active.
var aquery = context.Images;
var lquery = context.Students;
var result = from l in lquery
join a in aquery on l.Id equals a.StudentId into aGroup
from a in aGroup.OrderByDescending(m => m.ModifiedOn).Take(1)
select new {
l.id,
a.StudentId,
a.Status
};
This failed the dbset
s are not IEnumerable. Any idea how to get the correct result?