如何按分类标准对项目结果进行分组

时间:2017-05-03 08:02:16

标签: orchardcms

我有一个ContentType Animal,它有一个Taxonomy字段种类。 请查看此帖子http://orchardpros.net/tickets/10636和(http://www.ideliverable.com/blog/ways-to-render-lists-of-things)以获得更好的解释。 我破坏了代码,但它有2个错误。 1.var speciesField = item.Animal.Species; ----给了erro:CS1061," Orchard.ContentManagement.ContentItem'不包含Animal"的定义。 2.var items = speciesDictionary [speciesTerm]; ----给了erro:CS0136"名为' items'的本地或参数不能在此范围内声明,因为该名称用于封闭的本地。 请帮忙!

@using Orchard.ContentManagement
@using Orchard.Taxonomies.Models

@{

    var items = ((IEnumerable<ContentItem>)Model.ContentItems);

    var speciesDictionary = new Dictionary<TermPart, IList<ContentItem>>();


    // Collect categories and their items.
    foreach (var item in items) {

        var speciesField = item.Animal.Species; // Assumes that the species field is attached to the Animal type's implicit part (Animal).
        var speciesTerms = (IEnumerable<TermPart>)speciesField.Terms;

        foreach (var speciesTerm in speciesTerms) {
            var list = speciesDictionary.ContainsKey(speciesTerm) ? speciesDictionary[speciesTerm] : default(IList<ContentItem>);

            if (list == null) {
                list = new List<ContentItem>();
                speciesDictionary.Add(speciesTerm, list);
            }

            list.Add(item);
        }
    }
}
<ul>
    @foreach (var speciesTerm in speciesDictionary.Keys) {
        var items = speciesDictionary[speciesTerm];
        <li>
           @speciesTerm.Name


        </li>
    }
</ul>

1 个答案:

答案 0 :(得分:0)

  1. 您的第一个错误表示您正在尝试获取不存在的npm install @ionic-native/core@3.6.1 --save 类的属性。这实际上是部分正确的,因为它确实存在于ContentItem上,但是因为它将它转换为ContentItem类,它会丢失动态属性。 您可以通过不转换为ContentItem类来修复此问题,而只是转换为动态:

    ContentItem
  2. 第二个错误表示您正在尝试创建已存在的变量。这很明显,因为在.cshtml的顶部,您已经定义了var items = ((IEnumerable<dynamic>)Model.ContentItems);

    items

    所以在底部给它一个不同的名字:

    @{
        // var items is defined here, you can not redefine it
        // as always in C#
        var items = ((IEnumerable<ContentItem>)Model.ContentItems);
    }
    
相关问题