by eru · 8y ago · 0 comments
The following gets executed on iteration (of expensiveOrders), the lambda is compiled into a Function and run on each item in orders:
IEnumerable<T> orders = ...;
const expensiveOrders = orders.Where(o => o.total > 100)
The following gets compiled into an expression tree, which an ORM can analyze and convert to SQL. Basically it's just "Code as Data". IQueryable<T> orders = ...;
const expensiveOrders = orders.Where(o => o.total > 100)
The language's ability to treat code as data allows the programmer to express queries in native language syntax and pass it to an ORM (such as EF or Linq to Sql) for execution on a data store.Add: Where does the 'O' part fit in? You build a entities (in plain C#) with relationships to each other, and you could do stuff like:
//Pseudo-code
db.Customers.Where(c => ...)
db.Save(customer);
Edit: clarified - executed on iteration, not immediately.(Logic programming might also be workable?)
var r = customers.Select(c => new { cust = c, orders = c.Orders })
This gives you an IEnumerable (basically, a forward-only sequence) of objects - but these objects are of an anonymous type that was implicitly defined by "new".Anyway, the typical C# ORM is Entity Framework, which includes the LINQ-to-Entities query provider.