2016-03-21 13 views
0

Ich habe eine SQL-Abfrage, die mir rund 30 Datensatz geben wird, aber die gleiche Abfrage, sobald ich auf linq Abfrage änderte es gibt mir Tausende von Datensatz. Ich kann nicht die eigentliche Ursache des Problems finden jemand mir helfen ...Linq Abfrage erhält mehr Datensätze als SQL-Abfrage

SQL-Abfrage

  select 
DLId = p.Id, 
TopicId = st.Id, 
TopicName = at.Name, 
PrimaryOrg = bo.BusinessUnit, 
StatusId = ns.ID, 
ModifiedBy = pa.LastName 

from STopics st 
join ATopics at on st.Id = at.Id 
join Students p on st.StudentId = p.Id 
join Sorgs sbu on at.BUorgID = sbu.BUOrgID 
join BOrgs bo on sbu.BUOrgID = bo.ID 
join Status ns on st.SID = ns.ID 
join Students pa on st.NominatedBy = pa.Email 
where p.IsActive = 1 and sbu.StudentID = 123 and sbu.IsActive = 1 

und die Linq-Abfrage ist

(from st in Context.STopics 
join at in Context.ATopics on st.Id equals at.Id 
join p in Context.Students on st.StudentId equals p.Id 
join sbu in Context.Sorgs on at.BUorgID equals sbu.BUOrgID 
join bo in Context.BOrgs on sbu.BUOrgID equals bo.ID 
join ns in Context.Status on st.SID equals ns.ID 
join pa in Context.Students on st.NominatedBy equals pa.Email 
where p.IsActive==true && sbu.StudentID == 123 && sbu.IsActive == true 
select new result() 
{ 
DLId = p.Id, 
    TopicId = st.Id, 
    TopicName = at.Name, 
PrimaryOrg = bo.BusinessUnit, 
StatusId = ns.ID, 
ModifiedBy = pa.LastName 
}) 
+1

StudentID <> TeilnehmerID –

+1

sry, das war ein Tippfehler. korrigiert. – poc

+7

Überprüfen Sie die [generierte SQL] (https://msdn.microsoft.com/en-us/library/bb386961%28v=vs.110%29.aspx) – Martheen

Antwort

0
(from st in Context.STopics 
join at in Context.ATopics on st.Id equals at.Id 
join p in Context.Students on new { st.StudentId, p.IsActive } equals new { p.Id , true} 
join sbu in Context.Sorgs on new { sbu.BUorgID, sbu.IsActive,sbu.StudentID } equals new { at.BUorgID , true, 123} 
join bo in Context.BOrgs on sbu.BUOrgID equals bo.ID 
join ns in Context.Status on st.SID equals ns.ID 
join pa in Context.Students on st.NominatedBy equals pa.Email 
select new result() 
{ 
DLId = p.Id, 
TopicId = st.Id, 
TopicName = at.Name, 
PrimaryOrg = bo.BusinessUnit, 
StatusId = ns.ID, 
ModifiedBy = pa.LastName 
}) 

Können Sie dies versuchen, ein, wenn Sie genaues Problem erhalten möchten, können Sie erhalten, wie SQL-Abfrage aus den Linq-Anweisungen generiert wird, Linq-Abfrage verwendet viele innere Abfrage-Methodik

var query= your linqquery; 
string sqlQuery=query.ToString(); 

you can review sqlQuery. 
Verwandte Themen