3 ms·
C#'s Linq lets you treat queries as first class citizens. For instance: say you have: var customers = db.GetCollection<Customer>().AsQueryable(); Where Cust
by scarface74 8y ago
C#'s Linq lets you treat queries as first class citizens.
For instance:
say you have:
var customers = db.GetCollection<Customer>().AsQueryable();
Where Customer is a C# object and it can have nested objects.
When you right a query:
var seniors = from c in customers where c.Age > 30 select c;
Assuming that db is a Mongo context object and hover over the seniors variable at runtime, you will see the MongoQuery that is generated.
If you then write:
seniors = seniors.where(s => s.age < 90)
It will add the second predicate and modify the MongoQuery at runtime.
The query isn't actually run until you do something like
var oldCustomers = seniors.ToList();
If you went in and changed the customers initialization to an in memory List<Customers>, the same queries would still work, the ToList() it would generate actual code instead of a MongoQuery.
If customers were a reference to an Entity Framework SQL Server DbContext, the same Linq statements would generate SQL.
You can even write a method that takes in a Linq expression:
List<Customers> Find(Expression<Func<bool,Customer>>)
and call it
var oldFolks = Find(c => c.Age > 65)
C# translates Linq expressions to Expression Trees and then the Linq provider you choose (or write) parses the expression tree at runtime and converts it to the appropriate query language or for in memory lists, code.
You could switch out Mongo for an RDMS and all of the clients using the Find method would be non the wiser.
I guess OQL is a lot like Linq but since C# understand Linq natively you get strong typing, support for joins, etc.
- atombender 8y agoRight, this is a bit different and not really seamless (e.g. an object database wouldn't require a ToList()). The ability to express queries this way is nice, though.
- scarface74 8y agoThe idea of ToList(), .Count() etc., is that the query isn’t executed until you use such a function, you can keep composing the query and it will be translated and run on the server instead of the client. For instance.... var seniors = customers.Where(c => c.Age > 65).ToList() foreach(c in seniors) {} Will be translated to a query and run on the database server. var seniors = customers.ToList().Where(c => c.age > 65) foreach(c in customers) {} Will give you back the same results, but you will get back every record in the table/collection and the filtering will be done client side. Btw: The first query could be rewritten as var seniors = (from c in Customers where c.Age > 65).ToList(). You would only do .ToList() if you want to keep the scope of the connection small if for instance you are passing it outside of the class. You can iterate the results directly from the query instead of returning the results in memory (using ToList()) and it would work just like any other database style cursor. EDIT: Just to clarify from my first post, the example should have been List<Customers> Find(Expression<Func<bool,Customer>> predicate) { .... return customers.Where(predicate).ToList(); } This pattern also works great for unit testing. You can mock out your database Context and use a List<T> Customer to store data and you can still test your queries without having a dependency on a database.