简体   繁体   中英

How do I only get unique values to appear in a 2sxc list with a C# razor template?

Using 2sxc App, I have a listing of career cards. On each card, I have a CSS class assigned with a category so that I can filter the listing by using buttons in the sidebar. (using a javascript called mixitup).

Because of this, the CSS class "social-studies" might show up 10 times and Math might show up 1 time and Science might show up 7 times - depending on the number of times the category has been assigned to a career.

For the sidebar's filter buttons, I want to only show the filters that are used. So if my categories are:

  1. Social Studies
  2. Math
  3. Sciences
  4. Physical Education
  5. English
  6. History

But if only Social Studies, Math, and Sciences are used, I want to only show those 3.

Using the typical loop in 2sxc, I can loop through every instance but that creates a big list that has duplicate filter buttons. How can I create a loop in c# razor that only provides the unique instances of the filters?

This was easier than I thought. I discovered ".Distinct()"

@foreach(var career in AsList(Data)) { 
            foreach(var schoolCategory in AsList(career.Categories as object).Distinct()){
                <button type="button" data-filter="@Html.Raw(".filter-" + schoolCategory.SchoolCategory.ToLower().Replace(" ","-"))" class="badge badge-secondary">@Html.Raw(schoolCategory.SchoolCategory)</button>
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM