[英]Is it possible to utilize the LINQ portion of the foreach inside the loop?
我在ASP.NET MVC應用程序中有這個視圖:
<%
var path = Path.Combine(HttpRuntime.AppDomainAppPath, "uploads");
foreach (var file in Directory.GetFiles(path).OrderBy(f => new FileInfo(f).Length))
{
var item = new FileInfo(file);
%>
<tr>
<td></td>
<td>
<%=Html.Encode(Path.GetFileName(item.Name))%>
</td>
<td>
<%=Html.Encode(Functions.FormatBytes(item.Length))%>
</td>
<td>
<%=Html.FileLink(item.Name)%>
</td>
</tr>
<% } %>
是否有可能在循環內訪問我的變量f
,或者是否有其他方法來執行此操作,因此我不必維度FileInfo(file)
兩個實例?
謝謝!
var fileInfos = new DirectoryInfo(path).GetFiles().OrderBy(f => f.Length);
foreach (var fileInfo in fileInfos)
{
...
}
你的觀點在這里超越了它的責任。 您應該創建一個類,映射到要在視圖中顯示的數據,然后在控制器中,檢索數據並填充類的IEnumerable <>並在ViewModel中返回,您的視圖可以只是簡單循環通過。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.