获取与 Sitecore 中的 DropLink 选项匹配的所有项目

Get all items that match a DropLink option in Sitecore

这看起来应该很容易。也许是,我只是想多了。我有一堆通过 DropLink 设置类别字段的项目。我想抓取所有与这些选项之一匹配的项目。例如,获取 Category=Brochure 的所有项目的列表。我似乎无法获得 Droplink 选项的 ID 以匹配项目本身的类别选项。

编辑:按要求包含当前代码。

public List<PoolDownload> Manuals
        {
            get
            {
                LookupField cat = (LookupField)this.Item.Fields["Category"];
                return this.Downloads.Where(i => (i.Item.TemplateID == PoolDownload.TemplateId) &&
                                        (i.Item.GlassCast<Pdp.Pool.Website.Business.Entities.PoolDownload>().Category.ToString() == cat.TargetID.ToString()))
                                        .ToList();
            }
        }

我认为问题在于您将 Guid.ToString()Sitecore.Data.ID.ToString() 进行比较。这两个语句return不同的值:

var guidToString = Sitecore.Context.Item.ID.Guid.ToString();
// "2a6a1d9a-be1d-411b-821a-7e63775280b3"

var idToString = Sitecore.Context.Item.ID.ToString();
// "{2A6A1D9A-BE1D-411B-821A-7E63775280B3}"

也将 TargetID 转换为 Guid,你应该不错。

要在下面的评论中回答您关于显示按类别分组的 "Download Items" 的问题,您可以使用 GroupBy 方法,https://msdn.microsoft.com/en-us/library/bb534304(v=vs.110).aspx,如下所示:

public IEnumerable<IGrouping<Guid, PoolDownload>> Manuals
{
    get
    {
        LookupField cat = (LookupField)this.Item.Fields["Category"];

        return this.Downloads.Where(i =>
            i.Item.TemplateID == PoolDownload.TemplateId
            && i.Item.GlassCast<Pdp.Pool.Website.Business.Entities.PoolDownload>().Category.ToString() == cat.TargetID.Guid.ToString())
            .GroupBy(i => i.Category);
    }
}

然后,要遍历新手册 属性 中的结果,您可以这样做:

foreach(var categoryGroup in Manuals)
{
    var categoryGuid = categoryGroup.Key;

    foreach(var download in categoryGroup)
    {
        var downloadInCurrentGroup = download.Item;
    }
}