2009-03-02 9 views
1

私は一般的にLINQの新機能を使用していますが、私が望む結果を得るためにこれまでのことを把握しています。以下のLINQ2SQLクエリは、それぞれ独自の単位のコレクションを持つLocationオブジェクトがある望ましい結果を生成します。最初の変数宣言は、私が照会する必要のある値をセットアップするために行われ、問題には関係してはいけませんが、コンテキストのために必要です。LINQサブクエリを使用して子コレクションプロパティを設定する

私が抱えている問題は、400以上の場所があり、現在の方法でクエリが構造化されているということです。大きなクエリではなくデータベースごとにヒットしています。これはパフォーマンスが大きく低下し、完全な結果が戻ってくるまでに30秒以上かかるため、受け入れられません。私は2番目または2つの結果を返すストアドプロシージャの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()) 

UPDATE(2009年3月3日10:58): 私は、以下のクエリを使用して戻って1つのクエリ内のデータを得ることができたが、それは平らなテーブルの結果です。どのように私はこれを組織することが各場所の下で階層化されるように整形することができますか?私は「グループ参加」のようなものを使いたいと思っていますが、その仕組みに精通していません。位置情報は「OrgID」列までのすべてです。私はこのデータを、その場所にある組織のコレクションである「単位」プロパティを持つ場所のコレクションに整形する必要があります。どんな指導?

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}) 

答えて

0

だから私は物事をやり直すことになったとだけはかなり高速二回データベースを照会している、次の解決策を考え出した:あなたの助けのためのcasperOne

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()) 

ありがとう!

1

代わりList<T>として部材を露出させる、IEnumerable<T>又はIQueryable<T>のプロパティタイプを変更します。その後、ToListとクエリの呼び出しを削除して、を一度にサーバー上で完全に実行できるようにすることができます。

また、この種のコードは、LINQ to SQLがIMOを分解し始める場所です。私はあなたが望む結果を与えるストアドプロシージャを持っているなら、それを使用して、LINQ to SQLに返されたデータのあなたのオブジェクトへのマッピングを処理させるだけでよいと思います。

+0

私はそれをすべて1つのクエリで取り戻すことができました。編集した投稿を見て、私が1つの質問に悩まされている場所を確認してください。ありがとう! – Tom

関連する問題