Asp.net mvc 2 ASP.net MVC.2如何在存储库接口中定义多个类型的集合
我试图将一个列表返回到一个显示模板,其中包含来自两个表的数据,我使用repository模式和viewmodel将数据从返回两个对象的LINQ查询传递到视图 是否需要在接口中定义多个类型的集合? 对于单个类型,我在接口中使用了它Asp.net mvc 2 ASP.net MVC.2如何在存储库接口中定义多个类型的集合,asp.net-mvc-2,interface,repository,viewmodel,Asp.net Mvc 2,Interface,Repository,Viewmodel,我试图将一个列表返回到一个显示模板,其中包含来自两个表的数据,我使用repository模式和viewmodel将数据从返回两个对象的LINQ查询传递到视图 是否需要在接口中定义多个类型的集合? 对于单个类型,我在接口中使用了它 IQueryable<CustomerSite> CustomerSites { get; } public interface ICustomersAndSitesRepository { IQueryable<CustomerSi
IQueryable<CustomerSite> CustomerSites { get; }
public interface ICustomersAndSitesRepository
{
IQueryable<CustomerSite> CustomerSites { get; }
IQueryable<Customer> Customers { get; }
IQueryable<ICustomersAndSitesRepository> CustomerAndSites { get; }
}
将repository重新转换为视图模型时出现转换错误
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?)
无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.IEnumerable”。存在显式转换(是否缺少强制转换?)
我正在定义viewmodel,就像
公共IEnumerable客户{get;set;}
有人能给我指出正确的方向吗?返回一个iquirable自定义界面,其中包含您需要的每个集合的属性,怎么样
public interface ICustomersAndSites
{
IQueryable<CustomerSite> CustomerSites{get;}
IQueryable<Customers> Customers{get;}
}
公共接口ICcustomersandSites
{
IQueryable自定义项{get;}
IQueryable客户{get;}
}
然后,您可以在存储库中设置:
public IQueryable<ICustomersAndSites> CustomerAndSites
{
get { return combinationObject; }
}
公共IQueryable自定义站点
{
获取{return combinationObject;}
}
假设一个客户
有许多客户站点
,那么您将返回一个IQueryable
,但使用。包含以返回每个客户
中的客户站点
e、 g
public IQueryable GetCustomersAndSites()
{
返回MyEntities.Customers.Include(“CustomerSite”);
}
谢谢,我正在尝试实现这一点,但是.Include抛出一个错误,指出它未在客户实体中定义。Include是否需要一个名称空间才能工作?@Liam,您应该只需要使用System.Linq添加
和可能使用System.Data.Entity代码>。但是,请确保正确拼写Include实体,因为这是EDMX中显示的关系,而不是表名。如果是多对多关系,则可能是CustomerSites。@利亚姆,还要确保在连接字符串中为web.config
中的实体指定了MultipleActiveResultSets=True
。您好,谢谢,不过我在控制器中已经有Linq名称空间,data.entity one似乎没有显示,另外,当我在conn字符串中添加MultipleActiveResultSets=True时,它会声明该属性是不允许的。感谢您的建议,我正在尝试实现此功能,但无法正常工作,我已更新了问题。
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?)
public interface ICustomersAndSites
{
IQueryable<CustomerSite> CustomerSites{get;}
IQueryable<Customers> Customers{get;}
}
public IQueryable<ICustomersAndSites> CustomerAndSites
{
get { return combinationObject; }
}
public IQueryable<Customer> GetCustomersAndSites()
{
return MyEntities.Customers.Include("CustomerSite");
}