0
List<Filter> filters;
List<Deal> deals;
using (DealDataContext db = new DealDataContext())
{
    XElement xmlTree = XElement.Parse("<Request><ZipCode>92618</ZipCode></Request>");
    var result = db.SearchDeals(xmlTree);

    filters = result.GetResult<Filter>().ToList();
    deals = result.GetResult<Deal>().ToList();

}

return filters, deals;

What is the best way of returning more than one object?

dcolumbus
  • 9,596
  • 26
  • 100
  • 165

2 Answers2

4

Create a ViewModel look for best practices. Your case -

public class DealViewModel
{
List<Filter> filters{get; set;}
List<Deal> deals{get; set;}
}

Function -

DealViewModel vm= new DeakViewModel();

using (DealDataContext db = new DealDataContext())
{
    XElement xmlTree = XElement.Parse("<Request><ZipCode>92618</ZipCode></Request>");
    var result = db.SearchDeals(xmlTree);

    vm.filters = result.GetResult<Filter>().ToList();
    vm.deals = result.GetResult<Deal>().ToList();
    return vm;

}
Community
  • 1
  • 1
Vishal
  • 12,133
  • 17
  • 82
  • 128
  • Interesting. What I've got going on is a Repository that has a GetDeals function; the meat of that function is what I posted above. THEN I have a DealViewModel that just represents Deal and Filter get and set. – dcolumbus Dec 03 '10 at 18:58
  • Well change the type of GetDeals to return a ViewModel or get the info by separate functions return lists of each filters and deals and then assign them to your ViewModel Object. – Vishal Dec 03 '10 at 19:02
  • the first suggestion makes more sense. Returning the type of ViewModel. That way I only make one function call. Thank you! – dcolumbus Dec 03 '10 at 19:29
1

Create a class that wraps both objects up into one. Then return your wrapper object.

public class Wrapper
{
   List<Filter> Filters { get; set; }
   List<Deal> Deals { get; set; }
}
Brownman98
  • 1,053
  • 1
  • 7
  • 17