0

我正在使用实体框架在 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();

谢谢,

4

2 回答 2

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

于 2013-04-26T09:46:21.207 回答
0

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

于 2013-04-26T09:20:39.097 回答