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?

2 Answers 2

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;

}
Sign up to request clarification or add additional context in comments.

3 Comments

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.
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.
the first suggestion makes more sense. Returning the type of ViewModel. That way I only make one function call. Thank you!
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; }
}

3 Comments

Where should this wrapper go?
Okay, but I have a ViewModel... the issue is returning both filters and deals INTO the ViewModel.
@dcolumbus..Updated my response should help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.