0

I am new to the entity framework and am trying to convert the following query into the correct function calls.

Select Distinct a.nodeId FROM 
    (SELECT *
    FROM reportContents
    Where fitId = '29' and reportId = 
       (select max(reportId) 
       from reportContents
       where fitId = '29')
     ) a Where (a.nodeId IS NOT NULL)

I know this query does what i want, however i'm not sure how to translate that into the entitiy framework!

Here was my attempt.

var prevSelectedNodes = db.reportContents.Where(
f => f.fitId == id).Select(
f => f.nodeId).Distinct().ToList();

I need to somehow put a .Select() in the where call. However that kind of thing dosen't seem possible

Thank you in advance!

2
  • is reportId unique (pk)? Commented Jul 1, 2014 at 15:27
  • 2
    Looks like Ksven has already answered, but my advice if your new to EF is try not to think of it as querying a bunch of tables. EF is composed of objects, not relational tables. Commented Jul 1, 2014 at 15:28

1 Answer 1

2

As you can't make two LINQ nested lambda expression. You can do it with two requests :

var maxReportId = db.reportContents.Where(r => r.fitId = "29").Max(r => r.RepordId);
var result = db.reportContents.Where(r => r.fitId == "29" && r.reportId == maxReportId && r.nodeId != null).Select(a => a.nodeId).Distinct().ToList() ; 
Sign up to request clarification or add additional context in comments.

2 Comments

If you add the first linq statement into the second statement, I would expect it to generate a single SQL statement.
As written is should do it in one statement. If the first line was terminated with a .ToList() it would force it to be executed separately. Basically it gathers up the work until something actually needs the results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.