My Techie Bretheren(当然还有Sisteren!),
我有一个具有以下实体的LinqToSql数据模型: data model http://danimal.acsysinteractive.com/images/advisor.jpg
我需要检索特定办公室的所有顾问,按照他们在办公室内的顺序排序。我的第一部分是使用连接:
public static List<Advisor>GetOfficeEmployees(int OfficeID)
{
List<Advisor> lstAdvisors = null;
using (AdvisorDataModelDataContext _context = new AdvisorDataModelDataContext())
{
var advisors = from adv in _context.Advisors
join advisoroffice in _context.OfficeAdvisors
on adv.AdvisorId equals advisoroffice.AdvisorId
where advisoroffice.OfficeId == OfficeID
select adv;
lstAdvisors = advisors.ToList();
}
return lstAdvisors;
}
然而,我似乎无法围绕order by子句包裹疲惫的大脑。谁能提出一些建议?
答案 0 :(得分:0)
from adv in _context.Advisors
where adv.OfficeAdvisor.Any(off => off.OfficeId == officeID)
order adv by adv.OfficeAdvisor.First(off => off.OfficeId = officeID).Sequence
select adv;
答案 1 :(得分:0)
public static List<Advisor>GetOfficeEmployees(int OfficeID)
{
List<Advisor> lstAdvisors = null;
using (AdvisorDataModelDataContext _context = new AdvisorDataModelDataContext())
{
var advisors = from adv in _context.Advisors
join advisoroffice in _context.OfficeAdvisors
on adv.AdvisorId equals advisoroffice.AdvisorId
where advisoroffice.OfficeId == OfficeID
group adv by adv.OfficeId into g
order by g.Sequence
select g;
lstAdvisors = advisors.ToList();
}
return lstAdvisors;
}
注意:我目前无法在Visual Studio上测试它,但应该可以正常工作。
答案 2 :(得分:0)
您可以像这样添加order by子句:
var advisors = from adv in _context.Advisors
join advisoroffice in _context.OfficeAdvisors
on adv.AdvisorId equals advisoroffice.AdvisorId
where advisoroffice.OfficeId == OfficeID
orderby advisoroffice.Sequence // < -----
select adv;