Linq Group通过显示列表中的详细信息

时间:2018-10-08 06:56:01

标签: c# linq umbraco7

enter image description here

更新,我添加了一张图片以显示返回的内容 我正在尝试创建一个显示类别和制造商的菜单(可以从中获取数据),但无法从GroupBy的列表中获取产品。

我的代码是:

IEnumerable<IPublishedContent> getAllProducts = UmbracoAssignedContentHelper.PageContentByAlias("productCatalog").Children;

var result = getAllProducts.GroupBy(x => new { Manufacturer = x.GetPropertyValue<string>("manufacturer"), Category = x.GetPropertyValue<string>("category")})
    .Select(b => new ProductsGroupByTypeViewModel
    {
        GetAllProductsGroupByType   = b.Select(bn => bn.GetPropertyValue<IEnumerable<string>>("product").ToList()),
        Category                    = b.Key.Category,
        Manufacturer                = b.Key.Manufacturer

    }).ToList();

var listOfProducts = result.ToList();  

查看模型

public IEnumerable<List<string>> GetAllProductsGroupByType { get; set; }
public string Category { get; set; }
public string Manufacturer { get; set; }

我最后一次尝试是

<ul>
    @foreach (var data in Model)
    {
        <li>@data.GetAllProductsGroupByType</li> <---------What do I need to do here
    }
</ul> 

显示

  

System.Linq.Enumerable + WhereSelectEnumerableIterator 2[Umbraco.Core.Models.IPublishedContent,System.Collections.Generic.List 1 [System.String]]   System.Linq.Enumerable + WhereSelectEnumerableIterator 2[Umbraco.Core.Models.IPublishedContent,System.Collections.Generic.List 1 [System.String]]

如何向下钻取并显示产品数据?

任何帮助将不胜感激

至于Eldho的答案,我已经尝试过了,但是没有用。我得到了错误。

  

异常详细信息:System.ArgumentNullException:值不能为null。   来自GetAllProductsGroupByType = b.Select(bn => bn.GetPropertyValue<IEnumerable<string>>("product").ToList()),

2 个答案:

答案 0 :(得分:0)

我认为您不能按分组方式进行选择。也许下面的代码段工作

IEnumerable<IPublishedContent> getAllProducts = UmbracoAssignedContentHelper.PageContentByAlias("productCatalog").Children;

var result = getAllProducts.GroupBy(x => new { Manufacturer = x.GetPropertyValue<string>("manufacturer"), Category = x.GetPropertyValue<string>("category")})
    .Select(b => new ProductsGroupByTypeViewModel
    {
        GetAllProductsGroupByType   = new List<string>(),
        Category                    = b.Key.Category,
        Manufacturer                = b.Key.Manufacturer

    }).ToList();

froeach(var item in result)
{
    item.GetAllProductsGroupByType = getAllProducts.Where(en => en.manufacturer == item.Manufacturer && en.category == item.Category).Select(en => en.product).ToList();
}

//now you can use result as your answer

答案 1 :(得分:0)

尝试这样

IEnumerable<IPublishedContent> getAllProducts = UmbracoAssignedContentHelper.PageContentByAlias("productCatalog").Children;

var result = getAllProducts.GroupBy(x => new { Manufacturer = x.GetPropertyValue<string>("manufacturer"), Category = x.GetPropertyValue<string>("category")})
    .Select(b => new ProductsGroupByTypeViewModel
    {
        GetAllProductsGroupByType   = b.Select(bn => bn.GetPropertyValue<List<string>>("product")),
        Category                    = b.Key.Category,
        Manufacturer                = b.Key.Manufacturer

    }).ToList();

var listOfProducts = result.ToList();  

数据模型

public List<List<string>> GetAllProductsGroupByType { get; set; }
public string Category { get; set; }
public string Manufacturer { get; set; }

查看

@foreach (var data in Model)
{
    if(data.GetAllProductsGroupByType != null || data.GetAllProductsGroupByType.Count == 0)
    {
        continue;
    }

    foreach(var item in data.GetAllProductsGroupByType)
    {
       <li>item</li>
    }
}