如何以更好的方式用foreach编写这个LINQ

时间:2013-04-26 09:12:22

标签: asp.net-mvc-3 c#-4.0 linq-to-entities

我正在使用Entity框架在MVC3中进行项目。我有foreach的LINQ查询。一切都好。但是当数据量增加时,我遇到了性能问题。我对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();

谢谢,

2 个答案:

答案 0 :(得分:0)

您必须在第一个语句中使用join。有关如何执行此操作的示例,例如:Joins in LINQ to SQL

答案 1 :(得分: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