環境
visual studio2017
ASP.NET MVC5
Entity FrameWork 6.2
質問内容は上記の通りです。
以下のようにviewに持っていきたいデータを一括取得する為に一気に結合したテーブルがあります。
アプリケーションはページング処理を行っており、全てのデータを持ってくると重くなってくるため、取得分をskip、takeで制限しています。
var fulltables = (from entry in DB.t_entry //DB.t_selectionのentryidが重複しているデータを持ってきている、selectionidが最新のものを持ってきたい join selection in DB.t_selection on entry.EntryId equals selection.EntryId join phases in DB.m_phase on selection.PhaseId equals phases.PhaseId join jobcategories in DB.m_jobcategory on entry.JobCategoryId equals jobcategories.JobCategoryId join selectionstatuses in DB.m_selection_status on entry.SelectionStatusId equals selectionstatuses.SelectionStatusId join entryroutes in DB.m_entry_route on entry.EntryRouteId equals entryroutes.EntryRouteId join locations in DB.m_location on entry.LocationId equals locations.LocationId join interviewers in DB.t_interviewer on selection.SelectionId equals interviewers.SelectionId into tInteviewers orderby entry.EntryId descending select new { candidateid = entry.EntryId, candidatename = entry.CandidateName, candidatenamekana = entry.CandidateNameKana, locationIds = entry.LocationId, location = locations.LocationName, jobcategory = jobcategories.JobCategoryName, jobcateoryid = entry.JobCategoryId, entryroute = entryroutes.EntryRouteName, entryrouteid = entry.EntryRouteId, attribute = entry.IsFresher, phase = phases.PhaseName, phaseid = selection.PhaseId, selectionstatus = selectionstatuses.SelectionStatusName, selectuonstatusids = entry.SelectionStatusId, selectionlimit = selection.SelectionLimit, expectedjoinsdate = entry.JoinExpectedDate, department = entry.DepartmentName, adoptcost = entry.AdoptCost, adoptrecordmonth = entry.AdoptCostRecordMonth, adoptpaymentmonth = entry.AdoptCostPaymentMonth, createdate = entry.CreatedDate, checkweather = tInteviewers.OrderByDescending(x => x.InterviewerId).Where(x => x.SelectionId == selection.SelectionId && x.UserId == userId).Count() }) /*.Distinct(x => x.Entry_Id)を使いたい*/.Skip(PageNumber).Take(PageSize).ToList();
本題なのですがこの結合テーブルはクエリ式を用いてjoinを行っております。
しかしこのまま結合してしまうと以下のようにt_entryとt_selectionが1:Nになっている関係上t_selectionテーブルに値が複数入ってしまうと
entry_id
を外部キーとするt_selectionテーブルの値が重複してしまい。
fulltablesには重複し
(ER図)
(重複データ)
そこで上記のコードから重複削除を行うdistinct、もしくは
join selection in DB.t_selection on entry.EntryId equals selection.EntryId
部分に一意の値である’SelectionIdの最大値だけを結合する’結合条件を書き、結合させたいのですが、
distinctはそのまま使うと重複条件が多いためまた引数に何を書いてよいか分からず(x => x.EntryIdと書いてもオーバロードが違うのかうまくいかず)か、うまく重複削除してあげたいものを削除してくれません。
また結合条件を書くことに関しましては、クエリ式のサンプルが少なく’selectionIdの最大値の一つを取得する’といった結合条件の書き方が分かりません。
どのように対処すればよいでしょうか?
よろしくお願い致します。
追記分
int PageNumber = candidateFilterList.pageNumber * candidateFilterList.pageSize - candidateFilterList.pageSize ?? 0; int PageSize = candidateFilterList.pageSize ?? 20; /*追加分*/ var selectiontable = (from o in _base.phoenixDB.t_selection group o by o.EntryId into g orderby g.Key descending from item in g where item.EntryId == g.Max(x => x.EntryId) select item).Skip(PageNumber).Take(PageSize); //viewで仕様する全てのデータを取得 var fulltables = (from entry in _base.phoenixDB.t_entry join selection in selectiontable /*selectiontableをjoin*/ on entry.EntryId equals selection.EntryId join phases in _base.phoenixDB.m_phase on selection.PhaseId equals phases.PhaseId join jobcategories in _base.phoenixDB.m_jobcategory on entry.JobCategoryId equals jobcategories.JobCategoryId join selectionstatuses in _base.phoenixDB.m_selection_status on entry.SelectionStatusId equals selectionstatuses.SelectionStatusId join entryroutes in _base.phoenixDB.m_entry_route on entry.EntryRouteId equals entryroutes.EntryRouteId join locations in _base.phoenixDB.m_location on entry.LocationId equals locations.LocationId join interviewers in _base.phoenixDB.t_interviewer on selection.SelectionId equals interviewers.SelectionId into tInteviewers orderby entry.EntryId descending select new BaseEntry { candidateid = entry.EntryId, candidatename = entry.CandidateName, candidatenamekana = entry.CandidateNameKana, locationIds = (int)entry.LocationId, location = locations.LocationName, jobcategory = jobcategories.JobCategoryName, jobcateoryid = entry.JobCategoryId, entryroute = entryroutes.EntryRouteName, entryrouteid = entry.EntryRouteId, attribute = entry.IsFresher, phase = phases.PhaseName, phaseid = selection.PhaseId, selectionstatus = selectionstatuses.SelectionStatusName, selectuonstatusids = entry.SelectionStatusId, selectionlimit = selection.SelectionLimit, expectedjoinsdate = entry.JoinExpectedDate, department = entry.DepartmentName, adoptcost = entry.AdoptCost, adoptrecordmonth = entry.AdoptCostRecordMonth, adoptpaymentmonth = entry.AdoptCostPaymentMonth, createdate = entry.CreatedDate, checkweather = tInteviewers.OrderByDescending(x => x.InterviewerId).Where(x => x.SelectionId == selection.SelectionId && x.UserId == userId).Count() }).ToList(); /* Result => skipはメソッド 'Skip' は、LINQ to Entities では並べ替え済みの入力に対してのみサポートされます */


回答2件
あなたの回答
tips
プレビュー