Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将foreach重构为单个实例_C#_Asp.net Core_Foreach_Sql Order By - Fatal编程技术网

C# 如何将foreach重构为单个实例

C# 如何将foreach重构为单个实例,c#,asp.net-core,foreach,sql-order-by,C#,Asp.net Core,Foreach,Sql Order By,我注意到它总是只有一行要捕捉,所以我想将我的foreach重构为更简单的东西。我该怎么办 WorkShiftTask task = _context.WorkShiftTask.Include(a => a.InHoleSurvey).FirstOrDefault(e => e.WorkShiftTaskId == id); foreach (InHoleSurvey member in task.InHoleSurvey.OrderBy(a => a.WorkShiftT

我注意到它总是只有一行要捕捉,所以我想将我的foreach重构为更简单的东西。我该怎么办

WorkShiftTask task = _context.WorkShiftTask.Include(a => a.InHoleSurvey).FirstOrDefault(e => e.WorkShiftTaskId == id); 

foreach (InHoleSurvey member in task.InHoleSurvey.OrderBy(a => a.WorkShiftTask))
{
    //System.Diagnostics.Debug.WriteLine(member.InHoleSurveyId);
    TempData["hsId"] = member.InHoleSurveyId;
}
这个很好用

inHoleSurvey holes = _context.InHoleSurvey.Include(a => a.WorkShiftTask).
                 FirstOrDefault(i => i.WorkShiftTaskId == task.WorkShiftTaskId);

这只在我显式地给它URL ID时有效,但在我单击链接时无效。

如果你说的是
任务。InHoleSurvey
只包含一个元素,你不需要使用
foreach
,那么就不要使用
foreach

WorkShiftTask task = _context.WorkShiftTask.Include(a => a.InHoleSurvey).FirstOrDefault(e => e.WorkShiftTaskId == id); 
InHoleSurvey member = task.InHoleSurvey.FirstOrDefault();
TempData["hsId"] = member.InHoleSurveyId;

对不起,没有人知道你所说的“URL ID”和“点击链接”是什么意思。@robbpriestley问题不清楚,你是对的。但我只是想消除我对foreach的需求,谢谢。