2009-09-17 7 views
2

ASP.NET MVC 응용 프로그램에서이보기가 있습니다.루프 내부에서 foreach의 LINQ 부분을 활용할 수 있습니까?

<% 
    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)의?

고마워!

답변

6
var fileInfos = new DirectoryInfo(path).GetFiles().OrderBy(f => f.Length); 

foreach (var fileInfo in fileInfos) 
{ 
    ... 
} 
+0

ack, 가장 간단한 해결책은 항상 나를 피합니다. 고마워요 :) – Anders

2

귀하의 견해가 실제로 여기에 있습니다. 보기에 표시 할 데이터와 매핑 한 클래스를 만들고 컨트롤러에서 데이터를 검색하고 클래스의 IEnumerable <을 채우고 ViewModel에 반환하면 클래스가 단순 루프가 될 수 있습니다. 을 통하여.

+1

감사합니다 존, 내가 이것을 할 것입니다. MVC를 배우기 때문에 비평 해 주셔서 감사합니다 :) – Anders

관련 문제