score:1

Accepted answer

i would create a class that will hold gather all "store entities" and their sales, i think that using a collection of this class (storeentity in my example) it will be much easier to perform a variety of data manipulation (for this task and in future tasks).
here is my suggestion:

public class storeentity
{
    person personentity { get; set; }
    customer customerentity { get; set; }
    list<sale> saleslist { get; set; }

    public storeentity(person p, customer c,list<sale> sales)
    {
        this.personentity = p;
        this.customerentity = c;
        this.saleslist = sales;
    }

    public int salescount
    {
        get
        {
            if (saleslist == null)
            {
                return 0;
            }
            else
            {
                return saleslist.count;
            }
        }
    }
}

public list<storeentity> entities { get; set; }
public void mostorders()
{
    list<person> per = data.getallpersons();
    list<sale> sale = data.getallsales();
    list<customer> cus = data.getallcustomers();

    entities = new list<storeentity>();
    foreach (person p in per)
    {
        var cust = cus.where(x => x.personid == p.personid).select(x => x).singleordefault();
        if (cust != null)
        {
            var sales = sale.where(x => x.customerid == cust.customerid).tolist();
            storeentity entity = new storeentity(p, cust, sales);
            entities.add(entity);
        } 
    }

    // now you have list of all entities with their sales
    // it will be musch easy to manipulate with linq
    // you can do this:
    entities = entities.orderby(x => x.salescount).tolist();
    // or this...
    var bestentity = entities.max(x => x.salescount);
}

Related Query

More Query from same tag