使用LINQ子查询填充子集合属性

时间:2009-03-02 17:54:25

标签: linq linq-to-sql

我对LINQ一般都是新手,但到目前为止我已经设法解决了我想要/需要的结果。下面的LINQ2SQL查询产生了所需的结果,其中有Location对象,每个对象都有自己的Units集合。初始变量声明用于设置我需要查询的值,并且不应与我的问题相关,但是上下文需要这些值。

我遇到的问题是有400多个位置,而且我现在的查询结构方式,它是针对每个位置而不是一个大查询来访问数据库。这导致性能受到很大影响,需要30秒以上才能返回完整的结果,这是不可接受的。我有一个存储过程SQL查询,只返回一两秒钟的结果。

有没有办法重新构建此查询,以便我不会为每个位置访问数据库?我确定它是由Units子查询上的额外“ToList()”引起的,但我不知道如何在没有ToList()的情况下对结果进行整形。任何性能调整都将是一个巨大的帮助。谢谢你的帮助!

Dim curNames1 = (From ons In dc.organization_names _
    Where ons.eff_date <= ssDate _
    Order By ons.eff_date Descending _
    Group ons By ons.organization_id Into gNames = Group _
    Select New With { _
    Key .Key = organization_id, _
    .Result = gNames.Take(1)}) _
    .SelectMany(Function(a) (a.Result))

Dim curLocs1 = (From oLocs In dc.organization_locations _
    Where oLocs.eff_date <= ssDate _
    Order By oLocs.eff_date Descending _
    Group oLocs By oLocs.organization_id Into gLocs = Group _
    Select New With { _
    Key .Key = organization_id, _
    Result = gLocs.Take(1)}) _
    .SelectMany(Function(a) (a.Result))

Dim curStatuses1 = (From oEDate In dc.organization_conversion_dates _
    Where oEDate.edate <= ssDate _
    Order By oEDate.edate Descending _
    Group oEDate By oEDate.organization_id Into gEDates = Group _
    Select New With { _
    Key .Key = organization_id, _
    .Result = gEDates.Take(1)}) _
    .SelectMany(Function(a) (a.Result))

Dim curCommand1 = (From oCommand In dc.organization_service_assigneds _
    Where oCommand.eff_date <= ssDate _
    Order By oCommand.eff_date Descending _
    Group oCommand By oCommand.organization_id Into gCmds = Group _
    Select New With { _
    Key .Key = organization_id, _
    .Result = gCmds.Take(1)}) _
    .SelectMany(Function(a) (a.Result))

Dim curComponent1 = (From oCompo In dc.organization_compos _
    Where oCompo.eff_date <= ssDate _
    Order By oCompo.eff_date Descending _
    Group oCompo By oCompo.organization_id Into gCompos = Group _
    Select New With { _
    Key .Key = organization_id, _
    .Result = gCompos.Take(1)}) _
    .SelectMany(Function(a) (a.Result))

ss.Locations = New ObservableCollection(Of Location)(( _
    From map In dc.MapBackgrounds Where map.MapID = options.Map _
    Let Locs = map.Schemes.SchemeLocations _
    From SchemeLoc In Locs _
    Where (options.Locations.Count = 0 Or _
        options.Locations.Contains(SchemeLoc.Location.ID)) _
    Select New Location With { _
    .ID = SchemeLoc.Location.ID, .Name = SchemeLoc.Location.Location, _
    .Y = SchemeLoc.Y, .X = SchemeLoc.X, _
    .Country = New Country With _
    {.ID = SchemeLoc.Location.Countries.ID, _
    .Name = SchemeLoc.Location.Countries.country}, _
    .State = If(SchemeLoc.Location.State = -1, Nothing, _
    New USState With {.ID = SchemeLoc.Location.States.ID, _
    .Name = SchemeLoc.Location.States.state, _
    .Abbreviation = SchemeLoc.Location.States.state}), _
    .Units = New ObservableCollection(Of MillitaryUnit)(( _
    From curLoc In curLocs1 _
    Where curLoc.location = SchemeLoc.Location.ID _
    From curName In curNames1 _
    Where curName.organization_id = curLoc.organization_id _
    And (options.UnitSizes.Count = 0 Or _
        options.UnitSizes.Contains(curName.UnitSize)) _
    And (options.UnitTypes.Count = 0 Or _
    options.UnitTypes.Contains(curName.UnitType)) _
    From curEDate In curStatuses1 _
    Where curEDate.organization_id = curLoc.organization_id _
    And (options.Statuses.Count = 0 Or _
    options.Statuses.Contains(curEDate.status)) _
    From curCmd In curCommand1 _
    Where curCmd.organization_id = curLoc.organization_id _
    And (options.Commands.Count = 0 Or _
    options.Commands.Contains(curCmd.service_assigned)) _
    From curCompo In curComponent1 _
    Where curCompo.organization_id = curLoc.organization_id _
    And (options.Components.Count = 0 Or _
    options.Components.Contains(curCompo.compo)) _
    From curTable In curLoc.Organization.organization_tables _
    Where curTable.organization_id = curLoc.organization_id _
    And (options.Tables.Count = 0 Or _
    (options.Tables.Contains(curTable.table_id) Or _
    curTable.Tables.Any(Function(a) (options.Tables.Contains(a.parent_id))))) _
        Select New MillitaryUnit With { _
        .ID = curLoc.organization_id, _
        .Name = curName.name, _
        .IconPath = curName.icon, _
        .ConversionStatus = curEDate.Status1.status, _
        .ServiceCommand = curCmd.Service_Assigneds.service_assigned, _
        .Component = curCompo.Components.compo}).Distinct().ToList())}).ToList())

更新(2009年3月3日上午10:58): 我设法使用下面的查询在一个查询中获取数据,但它是一个平面表结果。我如何塑造这个,以便组织的东西在每个位置变得分层?我相信我想使用类似“群组加入”的东西,但我不熟悉它是如何工作的。位置信息是“OrgID”列的所有内容。我需要将这些数据整形为一组Locations,每个Locations都有一个“Units”属性,该属性是该位置的组织的集合。任何指导?

Dim locationsquery = (From map In dc.MapBackgrounds Where map.MapID = 1 Let Locs = map.Schemes.SchemeLocations _
                     From SchemeLoc In Locs Join curLoc In curLocs1 On SchemeLoc.LocID Equals curLoc.location  _
                     Join curName In curNames1 On curLoc.organization_id Equals curName.organization_id _
                     Join curStatus In curStatuses1 On curLoc.organization_id Equals curStatus.organization_id _
                     Join curCommand In curCommand1 On curLoc.organization_id Equals curCommand.organization_id _
                     Join curComponent In curComponent1 On curLoc.organization_id Equals curComponent.organization_id _
                     Select New With {.LocationID = SchemeLoc.LocID, .X = SchemeLoc.X, .Y = SchemeLoc.Y, _
                                      .LocationName = SchemeLoc.Location.Location, _ 
                                      .CountryID = SchemeLoc.Location.Countries.id, .CountryName = SchemeLoc.Location.Countries.country, _
                                      .StateID = SchemeLoc.Location.State, .StateName = SchemeLoc.Location.States.state, .StateAbbrev = SchemeLoc.Location.States.state, _
                                      .OrgID = curLoc.organization_id, _
                                      .OrgName = curName.name, _
                                      .OrgLocID = curLoc.location, _
                                      .IconPath = curName.icon, _
                                      .ConversionStatus = curStatus.status1.status, _
                                      .CurCmd = curCommand.service_assigneds.service_assigned, _
                                      .CurCompo = curComponent.components.compo})

2 个答案:

答案 0 :(得分:1)

不要将该成员公开为List<T>,而是将该属性类型更改为IEnumerable<T>IQueryable<T>。然后,您可以删除对ToList的调用,并且您的查询应该能够一次性完全在服务器上执行。

此外,这种代码是LINQ to SQL开始崩溃的地方,IMO。我认为,如果你有一个存储过程可以提供所需的结果,那么你应该使用它,只需让LINQ to SQL处理返回数据到对象的映射。

答案 1 :(得分:0)

所以我最终重做了一些东西并提出了以下解决方案,它只查询数据库两次并且速度更快:

    Dim locationsOnly = (From map In dc.MapBackgrounds Where map.MapID = 1 Let Locs = map.Schemes.SchemeLocations _
                                  From SchemeLoc In Locs _
                                  Where (options.Locations.Count = 0 Or options.Locations.Contains(SchemeLoc.LocID)) _
                                  Select New With {.LocationID = SchemeLoc.LocID, .X = SchemeLoc.X, .Y = SchemeLoc.Y, _
                                      .LocationName = SchemeLoc.Location.Location, _
                                      .CountryID = SchemeLoc.Location.Countries.ID, .CountryName = SchemeLoc.Location.Countries.country, _
                                      .StateID = SchemeLoc.Location.State, .StateName = SchemeLoc.Location.States.state, .StateAbbrev = SchemeLoc.Location.States.state}).ToList()


Dim orgsOnly = (From curLoc In curLocs1 _
                    Join curName In curNames1 On curLoc.organization_id Equals curName.organization_id _
                    Join curStatus In curStatuses1 On curLoc.organization_id Equals curStatus.organization_id _
                    Join curCommand In curCommand1 On curLoc.organization_id Equals curCommand.organization_id _
                    Join curComponent In curComponent1 On curLoc.organization_id Equals curComponent.organization_id _
                    Join curTable In dc.organization_tables On curLoc.organization_id Equals curTable.organization_id _
                    Where (options.UnitSizes.Count = 0 Or options.UnitSizes.Contains(curName.UnitSize)) _
                        And (options.UnitTypes.Count = 0 Or options.UnitTypes.Contains(curName.UnitType)) _
                        And (options.Statuses.Count = 0 Or options.Statuses.Contains(curStatus.status)) _
                        And (options.Commands.Count = 0 Or options.Commands.Contains(curCommand.service_assigned)) _
                        And (options.Components.Count = 0 Or options.Components.Contains(curComponent.compo)) _
                        And (options.Tables.Count = 0 Or (options.Tables.Contains(curTable.table_id) Or curTable.Tables.Any(Function(a) (options.Tables.Contains(a.parent_id))))) _
                    Select New With {.OrgLocID = curLoc.location, _
                       .MilUnit = New MillitaryUnit With { _
                       .ID = curLoc.organization_id, _
                       .Name = curName.name, _
                       .IconPath = curName.icon, _
                       .ConversionStatus = curStatus.Status1.status, _
                       .ServiceCommand = curCommand.Service_Assigneds.service_assigned, _
                       .Component = curComponent.Components.compo}}).Distinct().ToList()


ss.Locations = New System.Collections.ObjectModel.ObservableCollection(Of Location)((From loc In locationsOnly _
                    Group Join org In orgsOnly On loc.LocationID Equals org.OrgLocID Into Orgs = Group _
                    Select New Location With { _
                        .ID = loc.LocationID, _
                        .Name = loc.LocationName, _
                        .X = loc.X, _
                        .Y = loc.Y, _
                        .Country = New Country With {.ID = loc.CountryID, .Name = loc.CountryName}, _
                        .State = New USState With {.ID = loc.StateID, .Name = loc.StateName, .Abbreviation = loc.StateAbbrev}, _
                        .Units = New System.Collections.ObjectModel.ObservableCollection(Of MillitaryUnit)((From curOrg In Orgs _
                            Select curOrg.MilUnit).ToList())}).ToList())

感谢您的帮助casperOne!