linq support - AlienEngineer/VirtualObjects GitHub Wiki

Linq Support

    IQueryable<T> query = Session.GetAll<T>();
    // Or
    IQueryable<T> query = Session.Query<T>();

All queries can be verified under "VirtualObjects.Tests.Queries" namespace.

Just to be clear, all Linq queries are translated into the proper SQL parameterized commands. The framework will only load what needs to be loaded from the data source.

To see the generated SQL just provide (to the session) a configuration instance that sets the Logger to Console.Out (or other) for debug purposes. In release versions your app should never have the logger setted. This could result in pour performance.

IQueryable Methods Supported

  • Where
  • First, FirstOrDefault, Single, SingleOrDefault, Last, LastOrDefault
  • Count, LongCount
  • Distinct
  • GroupBy
  • Sum, Min, Max, Average
  • Any
  • Contains
  • Join and GroupJoin
  • Union
  • Select
  • OrderBy, OrderByDescending and ThenBy
  • Take and Skip

Supported Projections

    // On group joins (full entities and collection load)
    from o in Query<Orders>()
    join od in Query<OrderDetails>() on o equals od.Order into ods
    select new { Order = o, Details = ods }
    
    // On Joins (full entities load)
    from o in Query<Orders>()
    join od in Query<OrderDetails>() on o equals od.Order
    join e in Query<Employee>() on o.Employee equals e
    select new { Order = o, Detail = od, Employee = e }

    // Specific members projection
    from o in Query<Orders>()
    join od in Query<OrderDetails>() on o.Freight equals od.UnitPrice
    select new { o.OrderId, od.UnitPrice, o.ShipCity }

    Query<Employee>().Select(e => new { e.EmployeeId, e.LastName, e.FirstName })

    // Agregated Projection
    Query<Employee>().GroupBy(e => e.City)
    .Select(e => new
    {
        City = e.Key, 
        Min = e.Min(o => o.EmployeeId),
        Max = e.Max(o => o.EmployeeId),
        Sum = e.Sum(o => o.EmployeeId),
        Count = e.Count(),
        Average = e.Average(o => o.EmployeeId)
    })

    // Calculated Projection
    Query<Employee>().GroupBy(e => e.City)
    .Select(e => new
    {
        Average = e.Sum(o => o.EmployeeId) / (e.Count()*1.0) * 100.0
    })
    
⚠️ **GitHub.com Fallback** ⚠️