我有这个SQL,它产生所需的结果:
select * from Categories as c
inner join Questions as q on c.Id = q.CategoryId
inner join Surveys as s on s.Id = q.SurveyId
where s.RoleId = 2我想把它转换成lambda表达式。
它的工作原理:
我正在尝试拉出整个调查,使用Category.Questions等(已经构建的循环)遍历结果。
在这方面的任何帮助都将不胜感激,因为我正试图回到.NET的场景后,超过5年的回避.提前谢谢你。
发布于 2013-01-23 17:39:12
如果你的意思是LINQ:
var query = (from c in dataContext.Categories
join q in dataContext.Questions on q.CategoryId = c.Id
join s in dataContext.Surveys on q.SurveyId = s.Id
where c.RoleId = 5
select new
{
[Your Fields]
});发布于 2013-01-23 17:41:48
对于这个特定的查询,我更喜欢写成没有lambada表达式的LINQ
var query=(from c in db.Categories from q in Questions from n in Surveys where c.Id ==
q.CategoryId && q.SurveyId==n.Id && n..RoleId == 2).ToList();发布于 2013-01-23 19:11:43
通过使用以下方法解决了这个问题:
List<Question> questions = db.questions.Where(q => q.Survey.RoleId == iroleId).ToList();并在我的视图中过滤结果集,使用:
foreach (Tracker.Models.Category category in Model.Select(x => x.Category).Distinct().ToList())
{
...
foreach (Tracker.Models.Question question in Model.Where(x => x.Survey.RoleId == ViewBag.UserRoleId && x.CategoryId == catLoop.Id).ToList())看起来比我想象的要混乱一些,但解决方案有效。一定有比这更好的方法,尽管.
https://stackoverflow.com/questions/14485808
复制相似问题