2011-01-27 116 views
2

有人可以幫助我把這個查詢循環變成一個有效的Linq查詢嗎?我將它加載到TreeView中,因此必須連接每個項目。包括也非常低效。延遲加載項目也不起作用。正因爲如此,這個查詢訪問數據庫的許多更多的時間比它應該更高效的LINQ查詢

public IQueryable<Client> GetTopLevelData(Guid agentGuid, int year) 
    { 
     var clients = from client in ObjectContext.Clients 
         join cbc in ObjectContext.Client_Bucket_Client on client.Client_GUID equals cbc.Client_GUID 
         join acb in ObjectContext.Agent_Client_Bucket on cbc.Client_Bucket_GUID equals acb.Client_Bucket_GUID 
         where acb.Agent_GUID == agentGuid 
         select client; 

     foreach (Client c in clients) 
     { 
      var transactions = ObjectContext.Transactions.Where(t => t.Client_GUID == c.Client_GUID && t.Year == year); 
      foreach (Transaction t in transactions) 
      { 
       t.Forms.Attach(ObjectContext.Forms.Where(f => f.Transaction_GUID == t.Transaction_GUID && f.Year == year)); //.OrderByDescending(fo => fo.Create_Date)); 
      } 
      c.Transactions.Attach(transactions); 
     } 

     return clients; 
    } 
+0

LINQ to SQL或LINQ to entities? – Adrian 2011-01-27 22:01:54

+0

LINQ to Entities。 – 2011-01-27 22:05:54

回答

1
public IQueryable<Client> GetTopLevelData(Guid agentGuid, int year) 
{ 
    var clients = 
     from client in ObjectContext.Clients 
     join cbc in ObjectContext.Client_Bucket_Client on client.Client_GUID equals cbc.Client_GUID 
     join acb in ObjectContext.Agent_Client_Bucket on cbc.Client_Bucket_GUID equals acb.Client_Bucket_GUID 
     where acb.Agent_GUID == agentGuid 
     select client; 

    var clientInfos = 
     from c in clients 
     select new 
     { 
      Client = c, 
      TransactionInfos = ObjectContext.Transactions 
       .Where(t => t.Client_GUID == c.Client_GUID && t.Year == year) 
       .Select(t => new 
       { 
        Transaction = t, 
        ToAttach = ObjectContext.Forms.Where(f => f.Transaction_GUID == t.Transaction_GUID && f.Year == year) //.OrderByDescending(fo => fo.Create_Date) 
       }) 
     }; 

    // Looping over this query will hit the database *once* 
    foreach (var info in clientInfos) 
    { 
     foreach (var transactionInfo in info.TransactionInfos) 
      transactionInfo.Transaction.Forms.Attach(transactionInfo.ToAttach); 

     info.Client.Transactions.Attach(info.TransactionInfos.Select(t => t.Transaction)); 
    } 

    // Return a queryable object; constructing a new query from this will hit the database one more time 
    return clients; 
} 
2

對於那些關心這是工作的最終代碼。

var clients = 
    from client in ObjectContext.Clients 
    join cbc in ObjectContext.Client_Bucket_Client on client.Client_GUID equals cbc.Client_GUID 
    join acb in ObjectContext.Agent_Client_Bucket on cbc.Client_Bucket_GUID equals acb.Client_Bucket_GUID 
    where acb.Agent_GUID == agentGuid 
    select client; 

     var clientInfos = 
      from c in clients 
      select new 
      { 
       Client = c, 
       TransactionInfos = ObjectContext.Transactions 
        .Where(t => t.Client_GUID == c.Client_GUID && t.Year == year) 
        .Select(t => new 
        { 
         Transaction = t, 
         ToAttach = ObjectContext.Forms.Where(f => f.Transaction_GUID == t.Transaction_GUID && f.Year == year) //.OrderByDescending(fo => fo.Create_Date); 
        }) 
      }; 

     // Looping over this query will hit the database *once* 
     foreach (var info in clientInfos) 
     { 
      foreach (var transactionInfo in info.TransactionInfos) 
      { 
       transactionInfo.Transaction.Forms.Attach(transactionInfo.ToAttach); 
      } 

      var tt = info.TransactionInfos.ToList(); //.Select(t => t.Transaction); 

      var trans = tt.Select(t => t.Transaction); 

      info.Client.Transactions.Attach(trans); 
     } 

     // Return a queryable object; constructing a new query from this will hit the database one more time 
     return clients; 

我不得不枚舉底部的列表以獲取Linq編譯器來獲取表單。