我有一个sql查询,它会给我大约30条记录但同样的查询一旦我改为linq查询它给我数千条记录。我无法找到问题的实际根本原因可以帮助我...
sql查询
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
和linq查询是
(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
})
答案 0 :(得分: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
})
你能试试这个,如果你想得到确切的问题,你可以从linq语句中获得如何生成sql查询,linq查询使用了大量的内部查询方法
var query= your linqquery;
string sqlQuery=query.ToString();
you can review sqlQuery.