0

我正在尝试对我的物品进行分组并包含一个 where 子句,但我不太确定将我的物品放在哪里。

这是我到目前为止所拥有的:

@{
var trust = new trusteeEntities();
var gen = (from g in trust.Documents               
           where g.doc_type == "Minutes"
           orderby g.meeting_date descending
           group g by g.meeting_date into f
           select g);

    foreach (var f in gen)
    {        
       <div class="documents">   
          <span class="date">@string.Format("{0:MMMM d, yyyy}", f.meeting_date)</span> 
            <p><a href="/@f.filename">@f.title</a></p>                
       </div>        
    }   
  }  
4

1 回答 1

1

您必须在分组后订购物品,因为GroupBy不保持顺序。此外,您选择了错误的项目。要选择组,请使用 selectf而不是 select g

from g in trust.Documents
where g.doc_type == "Minutes"   
group g by g.meeting_date into f  // Groups the items g into groups called g
orderby f.Key descending          // Orders the groups by their key (which corresponds to g.meeting_date)
select f                          // Selects the group

我也强烈建议您重命名变量:

from document in trust.Documents
where document.doc_type == "Minutes"   
group document by document.meeting_date into documentGroup  // Groups the items g into groups called g
orderby documentGroup.Key descending                        // Orders the groups by their key (which corresponds to document.meeting_date)
select documentGroup                                        // Selects the group

显示组(不确定那部分,因为我从未编写过 ASP.NET 代码或 HTML 代码):

foreach (var documentGroup in gen)
{        
   <div class="documents">   
      <span class="date">@string.Format("{0:MMMM d, yyyy}", documentGroup.Key)</span> 
      foreach (var document in documentGroup)
      {        
        <p><a href="/@document.filename">@f.title</a></p>                
      } 
   </div>        
} 

更新

鉴于 foreach 中的代码,我认为您不需要按日期对文档进行分组。如果是这样,Linq 查询是:

from document in trust.Documents
where document.doc_type == "Minutes"   
orderby document.meeting_date descending
select document
于 2013-07-22T16:24:27.343 回答