Lazy Load for multiple entities at a time We have a system with a non standard database solution. All trips to the DB are rather expensive. We cannot use entity framework.

Currently our lazy loading is on an entity by entity basis. So if I have a Customer and access their Orders object it only loads the Orders for that customer. 

Something like so

//DAL
public List&lt;Entities.Customer&gt; GetCustomersByIds(IEnumerable&lt;int&gt; ids)
{
    var customers = db.GetCustomersByIds(ids);
    foreach(var customer in customers)
    {
        customer.Orders = new ResetLazy&lt;List&lt;Entities.Order&gt;&gt;(() =&gt; 
            db.GetOrdersByCustomerId(c.Id));
    }
    return customers;
}


ResetLazy taken from here https://stackoverflow.com/a/6255398/102526

Ideally if we had a collection of Customers and accessed an Orders collection on one of the customers it would load all the Orders for all the Customers. (If not in a collection it would just load it's own orders)

//DAL
public List&lt;Entities.Customer&gt; GetCustomersByIds(IEnumerable&lt;int&gt; ids)
{
    var customers = db.GetCustomersByIds(ids);
    foreach(var customer in customers)
    {
        customer.Orders = new ResetLazy&lt;List&lt;Entities.Order&gt;&gt;(() =&gt; 
            GetOrdersByCustomersLazy(customers, customer.Id));
    }
    return customers;
}

protected List&lt;Entities.Order&gt; GetOrdersByCustomersLazy(
    List&lt;Entities.Customer&gt; customers, 
    int customerId)
{
    var orders = db.GetOrdersByCustomerIds(
        customers.Select(customer =&gt; customer.Id).AsEnumerable());
    foreach(var customer in customers)
    {
        customer.Orders = new ResetLazy&lt;List&lt;Entities.Order&gt;&gt;(() =&gt; {
            customer.Orders = new ResetLazy&lt;List&lt;Entities.Order&gt;&gt;(() =&gt; 
                GetOrdersByCustomersLazy(customers, customer.Id));
            return orders.Where(order =&gt; order.CustomerId == customer.Id).ToList();
        });
    }
    return customers.FirstOrDefault(customer =&gt; customer.Id == customerId).Orders.Value;
}


This seems to work well, but it is too complex and not generic.
You code seems to not doing what it is supposed to do.

Assume the following implementation (using the ResetLazy)

Customer and Order  

namespace Entities
{
    public class Customer
    {
        public int Id { get; set; }
        public ResetLazy&lt;List&lt;Entities.Order&gt;&gt; Orders {get;set;}
    }
    public class Order
    {
        public int Id { get; set; }
        public int CustomerId { get; set; }
    }
}


Simulated database  

public class TheDatabase
{
    public TheDatabase()
    {
        customers.Add(new Entities.Customer { Id = 1 });
        orders.Add(new Entities.Order { Id = 1, CustomerId = 1 });
        orders.Add(new Entities.Order { Id = 2, CustomerId = 1 });
        orders.Add(new Entities.Order { Id = 3, CustomerId = 1 });

        customers.Add(new Entities.Customer { Id = 2 });

        customers.Add(new Entities.Customer { Id = 3 });
        orders.Add(new Entities.Order { Id = 4, CustomerId = 3 });
        orders.Add(new Entities.Order { Id = 5, CustomerId = 3 });
    }
    private List&lt;Entities.Customer&gt; customers = new List&lt;Entities.Customer&gt;();
    private List&lt;Entities.Order&gt; orders = new List&lt;Entities.Order&gt;();
    public List&lt;Entities.Customer&gt; GetCustomersByIds(IEnumerable&lt;int&gt; ids)
    {
        return customers.Where(c =&gt; ids.Contains(c.Id)).ToList();
    }
    public IEnumerable&lt;Entities.Order&gt; GetOrdersByCustomerIds(IEnumerable&lt;int&gt; ids)
    {
        return orders.Where(c =&gt; IsContained(c.CustomerId, ids));
    }
    private bool IsContained(int id, IEnumerable&lt;int&gt; ids)
    {
        foreach (int currentId in ids)
        {
            if (id == currentId) { return true; }
        }
        return false;
    }
}  


And the class containing your methods  

class Program
{

    static void Main(string[] args)
    {
        Program p = new Program();
        IEnumerable&lt;int&gt; ids = new List&lt;int&gt;() { 1, 2, 3 };
        List&lt;Entities.Customer&gt; customers = p.GetCustomersByIds(ids);

        int customerId = 1;
        List&lt;Entities.Order&gt; orders = new List&lt;Entities.Order&gt;(customers.Where(c =&gt; c.Id == customerId).First().Orders.Value);
        System.Diagnostics.Debug.Assert(orders[0].CustomerId == customerId);
    }

    TheDatabase db;
    private Program()
    {
        db = new TheDatabase();
    }

    public List&lt;Entities.Customer&gt; GetCustomersByIds(IEnumerable&lt;int&gt; ids)
    {
        var customers = db.GetCustomersByIds(ids);
        foreach (var customer in customers)
        {
            customer.Orders = new ResetLazy&lt;List&lt;Entities.Order&gt;&gt;(() =&gt;
                GetOrdersByCustomersLazy(customers, customer.Id));
        }
        return customers;
    }

    protected List&lt;Entities.Order&gt; GetOrdersByCustomersLazy(
        List&lt;Entities.Customer&gt; customers,
        int customerId)
    {
        var orders = db.GetOrdersByCustomerIds(
            customers.Select(customer =&gt; customer.Id).AsEnumerable());
        foreach (var customer in customers)
        {
            customer.Orders = new ResetLazy&lt;List&lt;Entities.Order&gt;&gt;(() =&gt;
            {
                customer.Orders = new ResetLazy&lt;List&lt;Entities.Order&gt;&gt;(() =&gt;
                    GetOrdersByCustomersLazy(customers, customer.Id));
                return orders.Where(order =&gt; order.CustomerId == customer.Id).ToList();
            });
        }
        return customers.FirstOrDefault(customer =&gt; customer.Id == customerId).Orders.Value;
    }
}


The Debug.Assert() method signals the wrong id.  

Fixing  

By adding a setter to ResetLazy.Value like  

set
{

    if (mode != LazyThreadSafetyMode.None)
    {
        lock (syncLock)
        {
            this.box = new Box(value);
        }
    }
    else
    {
        this.box = new Box(value);
    }
}


and changing the GetOrdersByCustomersLazy() method to  

protected List&lt;Entities.Order&gt; GetOrdersByCustomersLazy(
    List&lt;Entities.Customer&gt; customers,
    int customerId)
{
    var orders = db.GetOrdersByCustomerIds(
        customers.Select(customer =&gt; customer.Id).AsEnumerable());
    foreach (var customer in customers)
    {
        customer.Orders.Value = orders.Where(o =&gt; o.CustomerId == customer.Id).ToList();
    }
    return customers.FirstOrDefault(customer =&gt; customer.Id == customerId).Orders.Value;
}  


The following runs as expected  

Program p = new Program();
IEnumerable&lt;int&gt; ids = new List&lt;int&gt;() { 1, 2, 3 };
List&lt;Entities.Customer&gt; customers = p.GetCustomersByIds(ids);

int customerId = 1;
List&lt;Entities.Order&gt; orders = new List&lt;Entities.Order&gt;(customers.Where(c =&gt; c.Id == customerId).First().Orders.Value);
System.Diagnostics.Debug.Assert(orders.Count==3 &amp;&amp; orders[0].CustomerId == customerId);

customerId = 2;
orders = new List&lt;Entities.Order&gt;(customers.Where(c =&gt; c.Id == customerId).First().Orders.Value);
System.Diagnostics.Debug.Assert(orders.Count==0);

customerId = 3;
orders = new List&lt;Entities.Order&gt;(customers.Where(c =&gt; c.Id == customerId).First().Orders.Value);
System.Diagnostics.Debug.Assert(orders.Count == 2 &amp;&amp; orders[0].CustomerId == customerId);




You shouldn't shorten names of variables. The advantage of typing less is reduced by the lack of readability.  



A call to the linq Select() method returns an IEnumerable so a call to AsEnumerable() is not needed.