現在、プロジェクトに次のコードがあります
public List<PermissionValue> GetUnderlyingPermissionsForUser(string userName, int guiPermissionTypeId, int productTypeId)
{
using (CliVeEntities db = new CliVeEntities())
{
var listUnderlyings = (from gui in db.GuiPermissionUnderlying
join bue in db.BranchUsersExternal on gui.BranchUsersId equals bue.BranchUsersID
join bu in db.BranchUsers on bue.BranchUsersID equals bu.BranchUsersId
where bu.UserName == userName && gui.ViewTypeId == guiPermissionTypeId
&& gui.UnderlyingId.HasValue
&& !gui.SectorId.HasValue
&& gui.ProductTypeId == null
&& gui.ProductGroupId == null
select new PermissionValue { Id = gui.UnderlyingId.Value, Visible = gui.value}).ToList<PermissionValue>();
var listUnderlyingsProductGroup = (from gui in db.GuiPermissionUnderlying
join bue in db.BranchUsersExternal on gui.BranchUsersId equals bue.BranchUsersID
join bu in db.BranchUsers on bue.BranchUsersID equals bu.BranchUsersId
join pg in db.ProductGroup on gui.ProductGroupId equals pg.ProductGroupId
join pt in db.ProductType on pg.ProductGroupId equals pt.ProductGroupId
where bu.UserName == userName && gui.ViewTypeId == guiPermissionTypeId
&& gui.UnderlyingId.HasValue
&& !gui.SectorId.HasValue
&& pt.ProductTypeId == productTypeId
select new PermissionValue { Id = gui.UnderlyingId.Value, Visible = gui.value }).ToList<PermissionValue>();
var listUnderlyingsProductType = (from gui in db.GuiPermissionUnderlying
join bue in db.BranchUsersExternal on gui.BranchUsersId equals bue.BranchUsersID
join bu in db.BranchUsers on bue.BranchUsersID equals bu.BranchUsersId
where bu.UserName == userName && gui.ViewTypeId == guiPermissionTypeId
&& gui.UnderlyingId.HasValue
&& !gui.SectorId.HasValue
&& gui.ProductTypeId == productTypeId
select new PermissionValue { Id = gui.UnderlyingId.Value, Visible = gui.value }).ToList<PermissionValue>();
var listUnderlyingsForSectors = (from gui in db.GuiPermissionUnderlying
join bue in db.BranchUsersExternal on gui.BranchUsersId equals bue.BranchUsersID
join bu in db.BranchUsers on bue.BranchUsersID equals bu.BranchUsersId
join u in db.Underlying on gui.SectorId equals u.SectorId
where bu.UserName == userName && gui.ViewTypeId == guiPermissionTypeId
&& !gui.UnderlyingId.HasValue
&& gui.ProductTypeId == null
&& gui.ProductGroupId == null
select new PermissionValue { Id = u.UnderlyingId, Visible = gui.value }).ToList<PermissionValue>();
var listUnderlyingsForSectorsProductGroup = (from gui in db.GuiPermissionUnderlying
join bue in db.BranchUsersExternal on gui.BranchUsersId equals bue.BranchUsersID
join bu in db.BranchUsers on bue.BranchUsersID equals bu.BranchUsersId
join u in db.Underlying on gui.SectorId equals u.SectorId
join pg in db.ProductGroup on gui.ProductGroupId equals pg.ProductGroupId
join pt in db.ProductType on pg.ProductGroupId equals pt.ProductGroupId
where bu.UserName == userName && gui.ViewTypeId == guiPermissionTypeId
&& !gui.UnderlyingId.HasValue
&& pt.ProductTypeId == productTypeId
select new PermissionValue { Id = u.UnderlyingId, Visible = gui.value }).ToList<PermissionValue>();
var listUnderlyingsForSectorsProductType = (
from gui in db.GuiPermissionUnderlying
join bue in db.BranchUsersExternal on gui.BranchUsersId equals bue.BranchUsersID
join bu in db.BranchUsers on bue.BranchUsersID equals bu.BranchUsersId
where bu.UserName == userName && gui.ViewTypeId == guiPermissionTypeId
&& !gui.UnderlyingId.HasValue
&& gui.SectorId.HasValue
&& gui.ProductTypeId == productTypeId
select new PermissionValue { Id = gui.UnderlyingId.Value, Visible = gui.value }).ToList<PermissionValue>();
var mergeList1 = MergeAndDistinctList(listUnderlyings, listUnderlyingsProductType);
var mergeList2 = MergeAndDistinctList(mergeList1, listUnderlyingsProductGroup);
var mergeList3 = MergeAndDistinctList(mergeList2, listUnderlyingsForSectors);
var mergeList4 = MergeAndDistinctList(mergeList3, listUnderlyingsForSectorsProductType);
var resultMergeList = MergeAndDistinctList(mergeList4, listUnderlyingsForSectorsProductGroup);
return resultMergeList;
そして、これは私の MergeAndDistinctList 関数のコードです
protected List<PermissionValue> MergeAndDistinctList(List<PermissionValue> listPrimary, List<PermissionValue> listSecondry)
{
List<PermissionValue> listMergedAndDistinct = new List<PermissionValue>();
listMergedAndDistinct.AddRange(listPrimary);
var filter = listPrimary.Select<PermissionValue, int>(p => p.Id);
listMergedAndDistinct.AddRange(listSecondry.Where<PermissionValue>(p => !filter.Contains(p.Id)).Select(p => p));
return listMergedAndDistinct;
}
GetUnderlyingsForClient() コードに関する私の問題は、データベースに複数回ヒットすることです。このメソッドが頻繁にヒットすると、データベースへの複数回の呼び出しにつながる可能性があります。
このコードをより効率的にし、私がしなければならない DB 呼び出しの量を減らす方法を知っている人はいますか?