我认为可以将循环中的多个 linq 查询保存在单个变量中吗?
public ActionResult Index()
{
string ActiveUserId = (string)Session["ActiveUserGuid"];
Guid MyGuid = new Guid(ActiveUserId);
var queryRoots = from r in db.Roots
where r.UserId == MyGuid
select r.TaskId;
//Here is the questionable Part
foreach (var i in queryRoots)
{
var queryAllTasks = from t in db.Tasks
join b in db.Trees on t.TaskId equals b.ChildId
where b.TaskId == i
select t;
tasksForView.Add(queryAllTasks); <---????? obviously doesnt work
};
return View(queryAllTasks);
}
public class Root
{
public int RootId { get; set; }
public Guid UserId { get; set; }
public User User { get; set; } //navigation Property
public int TaskId { get; set; } // composite key
public int ChildId { get; set; }
public Tree Tree { get; set; } //navigation Property
}
public class Tree
{
public int TaskId { get; set; }
public int ChildId { get; set; }
public int Length { get; set; } //Path length
public virtual ICollection<Root> Roots { get; set; }
}
如果您想知道它应该做什么。我想从分层传递闭包表中查询几个子树并将它们返回到视图。
我知道我的代码还没有接近工作,我只是尝试了非常简单的解决方案!我想一种不同的查询方式可以一起解决这个问题