Skip to content

Instantly share code, notes, and snippets.

@vansha
Created February 24, 2013 21:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vansha/5025705 to your computer and use it in GitHub Desktop.
Save vansha/5025705 to your computer and use it in GitHub Desktop.
Querying complex object hierarchies with dapper.
public class Order {
public int Id { get; set; }
public DateTime OrderedAt { get; set }
public IList<OrderLine> OrderLines { get; set }
public Person Customer { get; set }
}
public class OrderLine {
public int Id { get; set; }
public int Count { get; set; }
public int Product { get; set; }
public decimal TotalPrice { get; set }
}
public class Product {
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
public class Person {
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
}
using (var conn = GetOpenConnection())
{
var query = QueryFor<Order>()
.Include(x => x.Customer)
.IncludeMany(x => x.OrderLines, opt => opt.Include(x => x.Product));
var sql = @"select *
from Order o
join Person p on p.Id = o.CustomerId
join OrderLines ol on ol.OrderId = o.Id
join Product pr on pr.Id = ol.ProductId
where o.OrderedAt > @LastCheckedAt")
var recentOrders = conn.Query(sql, query, new { LastCheckedAt = DateTime.UtcNow.AddDays(-1) });
}
@ZackStone
Copy link

ZackStone commented May 29, 2017

Hi! Where I find use the method QueryFor ?

@szszoke
Copy link

szszoke commented Jul 16, 2017

Hi! I am also curious about the purpose of QueryFor. Can you give us some update?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment