저는 Linq를 SQL에 사용하고 있으며 새 프로젝트에 Entity Framework를 사용하기 시작했습니다. LinqPad를 사용하여 Visual Studio에 통합하기 전에 코드를 테스트합니다. VS에서 디버깅하는 동안, 나는 나의 카운트가 다른 것을 알아 차렸다. VS에서 내 코드로 작성된 SQL을 검사했을 때 올바르게 변환되지 않은 것으로 나타났습니다.Entity Framework에서 C#을 SQL로 올바르게 변환하지 못했습니다.
VS 나의 코드 :
var adviceLineCallTotalViewModelList =
from a in db.AdviceLineCalls
.Include(a => a.Agency)
.Include(a => a.Staff)
.Include(a => a.StatusOfAdviceLineCaller)
.Include(a => a.AdviceLineCallSubjectMatter)
join ag in db.Agencies on a.AgencyNumber equals ag.AgencyNumber
join st in db.StatusOfAdviceLineCallers on a.StatusOfAdviceLineCallerID
equals st.StatusOfAdviceLineCallerID
join s in db.Staffs on a.StaffID equals s.StaffID
join sm in db.AdviceLineCallSubjectMatters on a.AdviceLineCallSubjectMatterID
equals sm.AdviceLineCallSubjectMatterID into grp
from sm in grp.DefaultIfEmpty()
where s.Employed == true
select new AdviceLineCallTotalViewModel()
{
AdviceLineCallID = a.AdviceLineCallID,
AdviceLineCallSubjectMatterID = sm.AdviceLineCallSubjectMatterID,
AdviceLineCallSubjectMatterDesc = sm.AdviceLineCallSubjectMatterDesc,
StatusOfAdviceLineCallerID = st.StatusOfAdviceLineCallerID,
StatusOfAdviceLineCallerDesc = st.StatusOfAdviceLineCallerDesc,
AgencyNumber = a.AgencyNumber,
AgencyNumberNameFacility = ag.AgencyNumberNameFacility,
CallDate = a.CallDate,
CallLength = a.CallLength,
Comments = a.Comments,
StaffID = a.StaffID,
LastName = s.LastName
};
내가 디버깅 및 생성 된 SQL을보고, 내가 볼 :
SELECT
[Extent1].[AdviceLineCallID] AS [AdviceLineCallID],
[Extent5].[AdviceLineCallSubjectMatterID] AS [AdviceLineCallSubjectMatterID],
[Extent5].[AdviceLineCallSubjectMatterDesc] AS [AdviceLineCallSubjectMatterDesc],
[Extent3].[StatusOfAdviceLineCallerID] AS [StatusOfAdviceLineCallerID],
[Extent3].[StatusOfAdviceLineCallerDesc] AS [StatusOfAdviceLineCallerDesc],
[Extent1].[AgencyNumber] AS [AgencyNumber],
[Extent2].[AgencyNumberNameFacility] AS [AgencyNumberNameFacility],
[Extent1].[CallDate] AS [CallDate],
[Extent1].[CallLength] AS [CallLength],
[Extent1].[Comments] AS [Comments],
[Extent1].[StaffID] AS [StaffID],
[Extent4].[LastName] AS [LastName]
FROM [dbo].[AdviceLineCall] AS [Extent1]
INNER JOIN [dbo].[Agency] AS [Extent2] ON [Extent1].[AgencyNumber] = [Extent2].[AgencyNumber]
INNER JOIN [dbo].[StatusOfAdviceLineCaller] AS [Extent3] ON [Extent1].[StatusOfAdviceLineCallerID] = [Extent3].[StatusOfAdviceLineCallerID]
INNER JOIN [dbo].[Staff] AS [Extent4] ON [Extent1].[StaffID] = [Extent4].[StaffID]
INNER JOIN [dbo].[AdviceLineCallSubjectMatter] AS [Extent5] ON [Extent1].[AdviceLineCallSubjectMatterID] = [Extent5].[AdviceLineCallSubjectMatterID]
WHERE 1 = [Extent4].[Employed]
마지막으로 "내부 조인"A "LEFT OUTER 가입"해야한다 줄 때문에 :
join sm in db.AdviceLineCallSubjectMatters on a.AdviceLineCallSubjectMatterID equals sm.AdviceLineCallSubjectMatterID into grp
from sm in grp.DefaultIfEmpty()
오른쪽 ???
참고 : "LEFT OUTER JOIN"이 포함되지 않은 이유에 대한 다른 게시물을 읽은 후 "Include"문을 포함 시켰습니다. 나는 "Includes"의 유무와 상관없이 동일한 결과를 얻습니다.
이전에 다른 간단한 쿼리에서 DefaultIfEmpty()를 사용했지만이 문제가 발생하지 않았습니다.
EF 초보자로서, 내가 틀린 일을하고 있는지, 아니면 내 프로젝트의 EF가 어떻게 손상되었는지 잘 모르겠습니다. 나는 EF 6.2를 사용하고있다.
는 편집 :
나는 새 Visual Studio 프로젝트를 만든 다음 코드를 사용 : 이 var adviceLineCallTotalViewModelList = from a in db.AdviceLineCalls
join ag in db.Agencies on a.AgencyNumber equals ag.AgencyNumber
join st in db.StatusOfAdviceLineCallers on a.StatusOfAdviceLineCallerID equals st.StatusOfAdviceLineCallerID
join s in db.Staffs on a.StaffID equals s.StaffID
join sm in db.AdviceLineCallSubjectMatters on a.AdviceLineCallSubjectMatterID equals sm.AdviceLineCallSubjectMatterID into grp
from sm_left in grp.DefaultIfEmpty()
where s.Employed == true
select new AdviceLineCallTotalViewModel()
{
AdviceLineCallID = a.AdviceLineCallID,
AdviceLineCallSubjectMatterID = sm_left == null ? 0 : sm_left.AdviceLineCallSubjectMatterID,
AdviceLineCallSubjectMatterDesc = sm_left == null ? String.Empty : sm_left.AdviceLineCallSubjectMatterDesc,
StatusOfAdviceLineCallerID = st.StatusOfAdviceLineCallerID,
StatusOfAdviceLineCallerDesc = st.StatusOfAdviceLineCallerDesc,
AgencyNumber = a.AgencyNumber,
AgencyNumberNameFacility = ag.AgencyNumberNameFacility,
CallDate = a.CallDate,
CallLength = a.CallLength,
Comments = a.Comments,
StaffID = a.StaffID,
LastName = s.LastName
};
이 행의 정확한 숫자 (5104)를 검색합니다. 또한 제대로 마지막은 LEFT OUTER로 조인 생성은 가입 :
LEFT OUTER JOIN [dbo].[AdviceLineCallSubjectMatter] AS [Extent5] ON [Extent1].[AdviceLineCallSubjectMatterID] = [Extent5].[AdviceLineCallSubjectMatterID]
하지만, 내 현재 프로젝트에서 코드의이 같은 줄은 5 개 레코드를 반환하고, 가입 지난 잘못 가입 내부로 변환됩니다.
현재 프로젝트에서 EF 또는 무언가가 손상되었다는 의미입니까? MVC와 EF의 초보자로서 무엇을해야할지 모르겠습니다. 당신의 AdviceLineCall 엔티티가 이미 관련 기관 (즉 a.AdviceLineCallSubjectMatter)에 대해 선언 된 참조가있는 경우
'join sm in db.AdviceLineCallSubjectMatters'와'gr from grp.DefaultIfEmpty()'에서 이상하게 보입니다. 두 번째'sm in'을'sm_left'로 바꾸고 int를 선택하여'AdviceLineCallSubjectMatterID = sm_left == null? 0 : sm_left.AdviceLineCallSubjectMatterID' – bradbury9
@ bradbury9 나는 당신이 제안한 변경을했다. 나는 아직도 같은 수를 얻는다. 그러나 'AdviceLineCallSubjectMatterDesc = sm.AdviceLineCallSubjectMatterDesc'를 주석 처리하면 정확한 카운트를 얻습니다. 참고 : 나는 Stack Overflow를 처음 사용하고이 주석의 코드 전후에 백틱을 올바르게 사용하고 있다고 생각합니다. "Add Comment?"를 클릭하면 코드 블록으로 표시됩니다. – MikeNaka
저는 bradbury9가 옳다는 것을 확신합니다. 조인과 출처의 태그로 "sm"을 사용해서는 안됩니다. 하나를 다른 것으로 변경하고에서 사용하는 태그 만 참조하십시오. "sm"및 "smgrp". 또한 개체에 할당 할 때 null 속성 참조를 사용합니다 (예 : "AdviceLineCallSubjectMatterID = smgrp? .AdviceLineCallSubjectMatterID ?? string.Empty"). –