var name = dbContext.Users .Where(u => u.Id == 123) .Select(u => u.Name) .First();
var name = dbContext.Users .Where(u => u.Id == 123) .Select(u => u.Name) .First();
I ask because `Where(u => ComplicatedBooleanFunction(u))` can't in general be transformed into something you can do in SQL, especially if it's stateful, but maybe there's a trick if the function is simple enough?
Some ORMs get around this problem with methods like .WhereEqual(User::id, 123) so you don't need to parse anything to know the semantics.
PS. I have in the past got praised at work for massively speeding up database access, when I fixed something like `List<User> userList = UserDAO.fetchAll(); return userList.length();`. You need a minimal understanding of databases to use an ORM correctly. The DAO even had a `.count()` method, but you need to know a little about databases to know to look for that in the first place.
Certain methods like First() in this case materialize expressions. Only those trigger a DB query.
An ORM in the traditional sense abstracts the details of the query itself fully away in favor of an object-oriented interface.
I'm not a C# user in my professional life so I'm happy to be corrected, BTW, just citing the source of my misinterpretation
The LINQ query in the comment above would only execute a single query like "SELECT name FROM users WHERE id = 123 LIMIT 1;" and would not even fetch the full entity, only the name.
The bit about lazy loading was a bit of a side-note more than my main point.
You are supposed to manually count the question marks in your raw SQL like the JDBC gods intended.
String sql = "INSERT INTO users (name, email, age, status, country) VALUES (?, ?, ?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "Alice");
pstmt.setString(2, "alice@example.com");
pstmt.setInt(3, 30);
pstmt.setString(4, "ACTIVE");
pstmt.setString(5, "US");
There is absolutely zero chance an ORM can ever get close to the performance of this query, so write it by hand!After all, the primary promise of ORMs has been that you do not have to write queries, it's right in the name! Object Relational Mapper!
String sql = "INSERT INTO users (name, email, age, status, country) " +
"VALUES (?, ?, ?, ?, ? )";
But I get your point.