views:

25

answers:

1

I'm sticking on how to best present some data that's being dynamically generated from two different tables.

Given my query:

    var assets = assetRepo.Find(x => x.LoginId == User.Identity.Name);
    var accounts = repository.Find(x => x.AccStatus == "A" && x.LoginId == User.Identity.Name);

    var query = from asst in assets
                join acct in accounts on asst.AccountId equals acct.AccountId
                select new
                {
                    Account = acct.AccountNumber,
                    Status = acct.AccStatus,
                    Make = asst.Make,
                    Model = asst.Model,
                    Submodel = asst.SubModel,
                    Registration = asst.Registration,
                    Balance = acct.BalanceOutstanding,
                    NextPayment = acct.NextPayment,
                    Date = String.Format("{0:dd MMM yyyy}", acct.NextPaymentDate),
                    Due = acct.ArrearsBal
                };

What would be the best (i.e. cleanest) way to bind this to the view? Would a custom class be required or is there a way to specify and iterate over a collection of anonymous types?

+1  A: 

Creating custom class can give you additional benefits. You can use DisplayAttribute to set column headers and order. Then you can create view (or template to use with DisplayFor) that takes list of objects of any type and uses reflection to read annotations and display view nicely.

class Report {
    [Display(Name="Account",Order=1)]
    public string Account {get; set;}

    [Display(Name="Next payment",Order=2)]
    public Date NextPayment {get; set;}
}

It looks also clean. You will be able to use this annotations not only for grid, but also for excel exports or other data operations.

LukLed