我有以下存储库方法: -
public RackJoin AllFindRackDetails_J(int id)
{
var tmsRack = tms.TMSRacks
.Include(a=>a.Zone)
.Include(a2=>a2.DataCenter)
.Include(a3=>a3.Technology)
.SingleOrDefault(a3=>a3.TMSRackID == id);
var IT360Rack = entities.Resources
.Include(a => a.ComponentDefinition)
.Include(a=>a.ResourceLocation.SiteDefinition.SDOrganization)
.Include(a=>a.ResourceLocation.SiteDefinition.AccountDefinition.SDOrganization)
// .Include(a=>a.CI)
.SingleOrDefault(a2=>a2.RESOURCEID == tmsRack.Technology.IT360ID);
RackJoin rj = new RackJoin {
Rack = tmsRack,
Resource = IT360Rack,
Technology = tmsRack.Technology,
ComponentDefinition = IT360Rack.ComponentDefinition,
Site = IT360Rack.ResourceLocation.SiteDefinition.SDOrganization,
Customer = IT360Rack.ResourceLocation.SiteDefinition.AccountDefinition.SDOrganization
};
return rj;
}
从以下操作方法调用: -
public ActionResult Details(int id,string detailName=null)
{
var racks = repository.AllFindRackDetails_J(id);
return View(racks);
}
我知道如果存储库方法返回IQurable<>然后,一旦我遍历Iqurable或调用.list(),数据库就会被命中。但是,当实际的数据库交互发生时,存储库方法只能返回单个对象会发生什么?
答案 0 :(得分:3)
SingleOrDefault
会导致查询立即执行。这适用于IQueryable<T>
上的所有LINQ-to-X扩展方法,这些方法返回IQueryable<T>
之外的其他内容。例如,First
,Count
或Max
都会立即执行。