0

I have a (Oracle)SQL query that I want to translate to Linq to use on a NHibernate context. What the query does is to get me all the Functions that has been deleted from a given Module (ie. they don't exist in the given module but are linked to a predecessor Module)

"SELECT * FROM Function WHERE ModID = " + module.PredecessorID + "
   AND FncName NOT IN
      (SELECT FncName FROM Function WHERE ModId = " + module.ModID + " )"

Here is a image to explain the data model

enter image description here

I'm looking for an expression with the "dot notation", something like this:

DBContext.GetAll<Function>()
    .Where(x => x.Module.ModID == module.PredecessorID)...

1 Answer 1

1

You could do this in two steps (for readability).

var excludedNames = DbContext.GetAll<Function>()
                             .Where(x => x.ModeID == module.ModID)
                             .Select(x => x.FncName);

var result = DBContext.GetAll<Function>()
                      .Where(x => x.Module.ModID == module.PredecessorID &&
                                  !excludedNames.Contains(x.FncName));
Sign up to request clarification or add additional context in comments.

4 Comments

Guess I could, but I would expect that NHibernate can't make that into only one SQL expression, and that would be preferable. (At least if feels like that would save some computational time right?)
@Markus : if you don't enumerate first query, this will be translated in one sql, not two.
don't enumerate? do you mean like var excludedNames instead of List<Function> excludedNames = ....ToList()?
well, you can use var, yes. Anyhow, if you don't put a .ToList() at the end, it's an IEnumerable<string> not a List<string> (we select FncName only, so it's no more a collection of Function.

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.