Im trying to return a list to a display template containing data from two tables, i am using the repository pattern, and a viewmodel to pass the data to the view from a LINQ query that returns both objects
is there to define a collection of multiple types in the interface?
For a single type i have used this in the interface,
IQueryable<CustomerSite> CustomerSites { get; }
then i have my linq query in the repository
public IQueryable<CustomerSite> CustomerSites
{
get { return customerSitesTable; }
}
However since this new query will contain 2 types, both customers and customer sites, how should this collection be defined in the interface?
======UPDATE
I have tried using the .Include (this throws an error stating that there is no definition for include in the Customer entity, not sure if im missing a name space for this?)
I have also tried creating a custom interface,
public interface ICustomersAndSitesRepository
{
IQueryable<CustomerSite> CustomerSites { get; }
IQueryable<Customer> Customers { get; }
IQueryable<ICustomersAndSitesRepository> CustomerAndSites { get; }
}
And this method in the repository
public IQueryable <ICustomersAndSitesRepository> CustomerAndSites
{
get { return CustomerAndSites; }
}
However when i try to pass the collection to the view in the controller like,
public ViewResult List([DefaultValue(1)] int page)
{
var customersWithSitesToShow = customersAndSitesRepository.CustomerAndSites;
var viewModel = new CustomerSitesListViewModel
{
Customers = customersWithSitesToShow.Skip((page - 1) * PageSize).Take(PageSize).ToList(),
PagingInfo = new PagingInfo
{
CurrentPage = page,
ItemsPerPage = PageSize,
TotalItems = customersWithSitesToShow.Count()
}
};
return View(viewModel); //Passed to view as ViewData.Model (or simply model)
}
I get a conversion error re convreting the repostiory to the view model
Cannot implicitly convert type 'System.Collections.Generic.List<CustomerDatabase.Domain.Abstract.ICustomersAndSitesRepository>' to 'System.Collections.Generic.IEnumerable<CustomerDatabase.WebUI.Models.CustomerViewModel>'. An explicit conversion exists (are you missing a cast?)
Im defining the viewmodel like
public IEnumerable Customers { get; set; }
Can anyone kindly point me in the right direction?
Assuming that a
Customerhas manyCustomerSitethen you’d return anIQueryable<Customer>but use.Includeto return theCustomerSiteswithin eachCustomer.e.g.