2013-04-26 42 views
0

我正在用Entity框架在MVC3中做項目。我有一個LINQ查詢與foreach。一切安好。但是當數據量增加時,我遇到了性能問題。我沒有太多的經驗與LINQ。所以我無法解決我的問題。請看看我的代碼,併爲我提供更好的建議。如何以更好的方式用foreach寫這個LINQ

代碼

 List<int> RouteIds = db.Cap.Where(asd => asd.Type == 3).Select(asd => asd.UserId).ToList(); 

     var UsersWithRoutingId = (from route in db.RoutingListMembers 
            where RouteIds.Contains(route.RoutingListId.Value) && route.User.UserDeptId == Id 
            select 
             new RoutingWithUser 
             { 
              UserId = route.UserId, 
              RoutingId = route.RoutingListId 
             }); 

     var ListRouteValue = (from cap in db.CareAllocationPercents 
           where cap.Type == 3 
           select new UserWithDeptId 
           { 
            Year = (from amt in db.CareAllocations where amt.CareItemId == cap.CareItemId select amt.Year).FirstOrDefault(), 
            UserId = cap.UserId, 
            UserDeptId = (from userdept in db.Users where userdept.Id == cap.UserId select userdept.UserDeptId).FirstOrDefault(), 

           }); 

     List<UserWithDeptId> NewRouteList = new List<UserWithDeptId>(); 
     ListRouteValue = ListRouteValue.Where(asd => asd.Year == Year); 

     foreach (var listdept in ListRouteValue) 
     { 
      foreach (var users in UsersWithRoutingId) 
      { 
       if (users.RoutingId == listdept.UserId) 
       { 
        UserWithDeptId UserwithRouteObj = new UserWithDeptId(); 
        UserwithRouteObj.UserId = users.UserId; 
        UserwithRouteObj.Year = listdept.Year; 
        UserwithRouteObj.UserDeptId = db.Users.Where(asd => asd.Id == users.UserId).Select(asd => asd.UserDeptId).FirstOrDefault(); 

        NewRouteList.Add(UserwithRouteObj); 
       } 
      } 
     } 
     NewRouteList = NewRouteList.Where(asd => asd.UserDeptId == Id).ToList(); 

感謝,

回答

0

你必須使用加入第一個語句。如何做到這一點的例子是,例如這裏:Joins in LINQ to SQL

0

我有一些想法給你: 第一: 小心完成你的地方接近到您的LINQ查詢得到的只是你需要什麼。 使用Linq on collection,可以刪除一個foreach循環。我不知道最終結果,但我試着爲你寫點東西:

var UsersWithRoutingId = (from route in db.RoutingListMembers 
             where RouteIds.Contains(route.RoutingListId.Value) && route.User.UserDeptId == Id 
             select 
              new RoutingWithUser 
              { 
               UserId = route.UserId, 
               RoutingId = route.RoutingListId 
              }); 

      var ListRouteValue = (from cap in db.CareAllocationPercents 
            where cap.Type == 3 
            select new UserWithDeptId 
            { 
             Year = (from amt in db.CareAllocations 
               where amt.CareItemId == cap.CareItemId && amt.Year == Year 
               select amt.Year).FirstOrDefault(), 
             UserId = cap.UserId, 
             UserDeptId = (from userdept in db.Users 
                where userdept.Id == cap.UserId && userdept.UserDeptId == Id 
                select userdept.UserDeptId).FirstOrDefault(), 

            }); 
List<UserWithDeptId> NewRouteList = new List<UserWithDeptId>(); 

      foreach (var listdept in ListRouteValue) 
      { 

       var user = UsersWithRoutingId.Where(uwri => uwri.RoutingId == listdept.UserId).FirstOrDefault(); 
       if (user != null) 
       { 
        NewRouteList.Add(new UserWithDeptId { UserId=user.UserId, Year=listdept.Year, UserDeptId=listdept.UserDeptId }); 
       } 
      } 

      return NewRouteList 

這對你適合嗎? (我不輪詢db.user表也得到了NewRouteList的UserDeptId假設一個在listdept是很好的)

二: 保重實體數據加載,如果你有表外鍵,如果不需要同時加載表格的子項,請注意刪除延遲加載。想象一下,使用外鍵指向其他人的多表的收益。 編輯: 這是一個鏈接,解釋它: http://msdn.microsoft.com/en-us/library/vstudio/dd456846%28v=vs.100%29.aspx

相關問題