Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何对此进行更好的LINQ查询_C#_Asp.net Mvc 3_Entity Framework - Fatal编程技术网

C# 如何对此进行更好的LINQ查询

C# 如何对此进行更好的LINQ查询,c#,asp.net-mvc-3,entity-framework,C#,Asp.net Mvc 3,Entity Framework,我正在用实体框架在MVC3中完成我的项目。我对林克没有多少经验。但我设法根据我的需求编写代码。现在,问题是,我的逻辑需要更多的时间来处理查询,因为我使用foreach分割了我的查询,并且在DB的单个列中有三种不同的类型。我试图使用LINQ本身编写完整的逻辑代码。但是没有帮我什么忙 我需要一个建议,如何更改逻辑以提高代码性能。请帮我做这个 行动方法代码 public decimal ReturnAmount(int Id, int Year) { var

我正在用实体框架在MVC3中完成我的项目。我对林克没有多少经验。但我设法根据我的需求编写代码。现在,问题是,我的逻辑需要更多的时间来处理查询,因为我使用foreach分割了我的查询,并且在DB的单个列中有三种不同的类型。我试图使用LINQ本身编写完整的逻辑代码。但是没有帮我什么忙

我需要一个建议,如何更改逻辑以提高代码性能。请帮我做这个

行动方法代码

        public decimal ReturnAmount(int Id, int Year)
    {

        var UsersWithDeptId = db.Users.Where(asd => asd.UserDeptId == Id).Select(asd => asd.Id).ToList();
        var ListUserValue = (from cap in db.CareAllocationPercents
                             where cap.Type == 1 && UsersWithDeptId.Contains(cap.UserId)
                             select new UserWithDeptId
                             {
                                 Year = (from amt in db.CareAllocations where amt.CareItemId == cap.CareItemId select amt.Year).FirstOrDefault(),
                                 Amount = (from amt in db.CareAllocations where amt.CareItemId == cap.CareItemId select amt.Amount).FirstOrDefault(),
                                 UserId = cap.UserId,
                                 UserDeptId = (from userdept in db.Users where userdept.Id == cap.UserId select userdept.UserDeptId).FirstOrDefault(),
                                 Percentage = cap.Percentage,
                                 CareItemId = cap.CareItemId,
                                 Category = "User",
                                 CareAllocationId = cap.Id
                             }).ToList();

        ListUserValue = ListUserValue.Where(asd => asd.Year == Year).ToList();

        List<int> RouteIds = db.CareAllocationPercents.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
                                      }).ToList();

        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(),
                                  Amount = (from amt in db.CareAllocations where amt.CareItemId == cap.CareItemId select amt.Amount).FirstOrDefault(),
                                  UserId = cap.UserId,
                                  UserDeptId = (from userdept in db.Users where userdept.Id == cap.UserId select userdept.UserDeptId).FirstOrDefault(),
                                  Percentage = cap.Percentage,
                                  Category = "Route",
                                  CareItemId = cap.CareItemId,
                                  CareAllocationId = cap.Id
                              }).ToList();

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

        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 = db.Users.Where(asd => asd.Id == user.UserId).Select(asd => asd.UserDeptId).FirstOrDefault(), Percentage = listdept.Percentage, CareItemId = listdept.CareItemId, Amount = listdept.Amount, CareAllocationId = listdept.CareAllocationId, Category = listdept.Category });
            }
        }

        NewRouteList = NewRouteList.Where(asd => asd.UserDeptId == Id).ToList();
        var ListUserId = (from user in db.Users
                          where user.UserDeptId == Id
                          select new UserWithDeptId
                          {
                              UserId = user.Id
                          }).ToList();

        var ListDeptId = (from cap in db.CareAllocationPercents
                          where cap.Type == 2 && cap.UserId == Id
                          select new UserWithDeptId
                          {
                              Year = (from amt in db.CareAllocations where amt.CareItemId == cap.CareItemId select amt.Year).FirstOrDefault(),
                              Amount = (from amt in db.CareAllocations where amt.CareItemId == cap.CareItemId select amt.Amount).FirstOrDefault(),
                              UserDeptId = cap.UserId,
                              Percentage = cap.Percentage,
                              Category = "Dept",
                              CareItemId = cap.CareItemId,
                              CareAllocationId = cap.Id,
                          }).ToList();

        ListDeptId = ListDeptId.Where(asd => asd.Year == Year).ToList();
        int UserCount = ListUserId.Count;
        List<UserWithDeptId> NewList = new List<UserWithDeptId>();

        foreach (var listdept in ListDeptId)
        {
            foreach (var users in ListUserId)
            {
                NewList.Add(new UserWithDeptId { UserId = users.UserId, UserDeptId = listdept.UserDeptId, Percentage = listdept.Percentage, CareItemId = listdept.CareItemId, Amount = listdept.Amount, CareAllocationId = listdept.CareAllocationId, Category = listdept.Category });
            }
        }
        int CountUser = ListUserValue.Count;
        int RouteCount = NewRouteList.Count;
        NewList.AddRange(ListUserValue);
        NewList.AddRange(NewRouteList);
        List<CAREReviewBefore> NewModelList = new List<CAREReviewBefore>();
        foreach (var mod in NewList)
        {
            CAREReviewBefore cr = new CAREReviewBefore();
           // int? BibId = (from pod in db.PoDetails where pod.Id == (from por in db.CareItems where por.Id == mod.CareItemId select por.PoReceipt.PoDetailId).FirstOrDefault() select pod.BibId).FirstOrDefault();
            int? InvoiceId = (from asd in db.PoReceipts
                              where asd.Id == (from careits in db.CareItems where careits.Id == mod.CareItemId select careits.PoRecId).FirstOrDefault()
                              select asd.InvoiceId).FirstOrDefault();

            //Current Currency Rate

            var Rate = db.Invoices.Where(In => In.Id == InvoiceId).Select(In => In.Rate).FirstOrDefault();
            var CurrencyRate = db.Invoices.Where(inv => inv.Id == InvoiceId).Select(inv => inv.Currency.Rate).FirstOrDefault();
            decimal Rat = 0;
            if (Rate != null || Rate != 0)
            {
                Rat = Convert.ToDecimal(Rate);
                if (Rat == 0)
                {
                    Rat = Convert.ToDecimal(CurrencyRate);
                }
                cr.Tot_Annual_SubCost = mod.Amount * Rat;
            }

            if (mod.Category == "User")
            {
                cr.Allocated_Cost_to_Dept = (((mod.Amount * mod.Percentage) / (100)) * Rat);
            }
            else if (mod.Category == "Dept")
            {
                cr.Allocated_Cost_to_Dept = (((mod.Amount * mod.Percentage) / (UserCount * 100)) * Rat);
            }
            else if (mod.Category == "Route")
            {
                cr.Allocated_Cost_to_Dept = (((mod.Amount * mod.Percentage) / (RouteCount * 100)) * Rat);
            }
            NewModelList.Add(cr);

        }
        var Amount = NewModelList.Sum(asd => asd.Allocated_Cost_to_Dept);
        return Amount;
    }
public decimal ReturnAmount(整数Id,整数年)
{
var UsersWithDeptId=db.Users.Where(asd=>asd.UserDeptId==Id);
var ListUserValue=(来自db.CareAllocationPercents中的cap
其中cap.Type==1&&UsersWithDeptId.Contains(cap.UserId)
选择新用户WithDeptid
{
年份=(从db.careeAllocations中的金额,其中amt.careeItemId==cap.careeItemId选择amt.Year)。FirstOrDefault(),
Amount=(从db.careeAllocations中的amt开始,其中amt.careeItemId==cap.careeItemId选择amt.Amount)。FirstOrDefault(),
UserId=cap.UserId,
UserDeptId=(从db.Users中的userdept开始,其中userdept.Id==cap.UserId选择userdept.UserDeptId)。FirstOrDefault(),
百分比=上限百分比,
CareeItemId=cap.CareeItemId,
Category=“用户”,
CareAllocationId=cap.Id
}).ToList();
ListUserValue=ListUserValue.Where(asd=>asd.Year==Year.ToList();
列出RouteIds=db.CareAllocationPercents.Where(asd=>asd.Type==3);
var UsersWithRoutingId=(来自db.RoutingListMembers中的路由
其中RouteIds.Contains(route.RoutingListId.Value)和&route.User.UserDeptId==Id
选择
新路由与用户
{
UserId=route.UserId,
RoutingId=route.RoutingListId
}).ToList();
var ListRouteValue=(来自db.CareAllocationPercents中的cap
其中cap.Type==3
选择新用户WithDeptid
{
年份=(从db.careeAllocations中的金额,其中amt.careeItemId==cap.careeItemId选择amt.Year)。FirstOrDefault(),
Amount=(从db.careeAllocations中的amt开始,其中amt.careeItemId==cap.careeItemId选择amt.Amount)。FirstOrDefault(),
UserId=cap.UserId,
UserDeptId=(从db.Users中的userdept开始,其中userdept.Id==cap.UserId选择userdept.UserDeptId)。FirstOrDefault(),
百分比=上限百分比,
Category=“路线”,
CareeItemId=cap.CareeItemId,
CareAllocationId=cap.Id
}).ToList();
List NewRouteList=新列表();
ListRouteValue=ListRouteValue.Where(asd=>asd.Year==Year.ToList();
foreach(ListRouteValue中的var listdept)
{
var user=UsersWithRoutingId.Where(uwri=>uwri.RoutingId==listdept.UserId).FirstOrDefault();
如果(用户!=null)
{
NewRouteList.Add(newuserWithDeptid{UserId=user.UserId,Year=listdept.Year,UserDeptId=db.Users.Where(asd=>asd.Id==user.UserId)。选择(asd=>asd.UserDeptId)。FirstOrDefault(),百分比=listdept.Percentage,CareItemId=listdept.CareItemId,金额=listdept.Amount,CareAllocationId=listdept.CareAllocationId,类别=listdept.Category});
}
}
NewRouteList=NewRouteList.Where(asd=>asd.UserDeptId==Id.ToList();
var ListUserId=(来自db.Users中的用户
其中user.UserDeptId==Id
选择新用户WithDeptid
{
UserId=user.Id
}).ToList();
var ListDeptId=(从db.CareAllocationPercents中的cap开始)
其中cap.Type==2&&cap.UserId==Id
选择新用户WithDeptid
{
年份=(从db.careeAllocations中的金额,其中amt.careeItemId==cap.careeItemId选择amt.Year)。FirstOrDefault(),
Amount=(从db.careeAllocations中的amt开始,其中amt.careeItemId==cap.careeItemId选择amt.Amount)。FirstOrDefault(),
UserDeptId=cap.UserId,
百分比=上限百分比,
Category=“Dept”,
CareeItemId=cap.CareeItemId,
CareAllocationId=cap.Id,
}).ToList();
ListDeptId=ListDeptId.Where(asd=>asd.Year==Year.ToList();
int UserCount=ListUserId.Count;
List NewList=新列表();
foreach(ListDeptId中的var listdept)
{
foreach(ListUserId中的var用户)
{
NewList.Add(new UserWithDeptId{UserId=users.UserId,UserDeptId=listdept.UserDeptId,Percentage=listdept.Percentage,careeitemid=listdept.careeitemid,Amount=listdept.Amount,CareAllocationId=listdept.CareAllocationId,Category=listdept.Category});
}
}
在里面
from cap in db.CareAllocationPercents
where (cap.Type == 1 && UsersWithDeptId.Contains(cap.UserId))
   || (cap.Type == 2 && cap.UserId == Id)
   || cap.Type == 3
select new UserWithDeptId
{
    Year = (from amt in db.CareAllocations where amt.CareItemId == cap.CareItemId select amt.Year).FirstOrDefault(),
    Amount = (from amt in db.CareAllocations where amt.CareItemId == cap.CareItemId select amt.Amount).FirstOrDefault(),
    UserId = cap.UserId,
    UserDeptId = (from userdept in db.Users where userdept.Id == cap.UserId select userdept.UserDeptId).FirstOrDefault(),
    Percentage = cap.Percentage,
    CareItemId = cap.CareItemId,
    Category = "User",
    CareAllocationId = cap.Id,
    Type = cap.Type
})