我正在使用实体框架在 MVC3 中做我的项目。我对LINQ没有太多经验。但我设法根据我的要求进行编码。现在,问题是,我的逻辑需要更多时间来处理查询,因为我使用foreach拆分了我的查询,并且在数据库的单列中有三种不同的类型。我试图使用 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;
}
谢谢,