Distinct Options

Is there a way to get distinct options? For example, if you had a list of names, and some names repeated themselves, is it possible to only list each name once?

I have tried using nameList.Select(x => x.Names).Distinct().ToList(); 

to try and get a list of distinct names from nameList But I cant quite figure it out. 

Thank you!

3 Replies 1 reply marked as answer

BC Berly Christopher Syncfusion Team March 9, 2021 05:23 PM UTC

Hi Nathanael, 
  
Greetings from Syncfusion support. 
  
We have checked the reported issue and distinct method is working fine at our end. So, we have prepared the sample and attached it below. Here, we have 10 items in the data source and having 3 duplicate values but we got the result count as 7 items. Please refer the below attached screenshot.  
  
@using Syncfusion.Blazor.DropDowns 
 
 
<SfMultiSelect @ref="DropdownObj" TValue="string[]" TItem="Games" Placeholder="Select a game" DataSource="@data"> 
    <MultiSelectFieldSettings Value="ID" Text="Text"></MultiSelectFieldSettings> 
</SfMultiSelect> 
 
@code { SfMultiSelect<string[], Games> DropdownObj; 
    public class Games 
    { 
        public string ID { get; set; } 
        public string Text { get; set; } 
    } 
    List<Games> data { get; set; } 
    List<string> LocalData { get; set; } 
    protected override void OnInitialized() 
    { 
        data = new List<Games> { 
            new Games() { ID= "Game1", Text= "American Football" }, 
            new Games() { ID= "Game2", Text= "American Football" }, 
            new Games() { ID= "Game3", Text= "American Football" }, 
            new Games() { ID= "Game4", Text= "Cricket" }, 
            new Games() { ID= "Game5", Text= "Football" }, 
            new Games() { ID= "Game6", Text= "Golf" }, 
            new Games() { ID= "Game7", Text= "Badminton" }, 
            new Games() { ID= "Game8", Text= "Basketball"}, 
            new Games() { ID= "Game9", Text= "Snooker" }, 
            new Games() { ID= "Game10", Text= "Football"}, 
        }; 
        this.LocalData = data.Select(x => x.Text).Distinct().ToList(); 
    } 
 } 
 
Screenshot: 
 
 
 
 
Please check the above sample and still issue persists, please share any simple issue reproducing sample or modify the sample with the reported issue that will help us to check and proceed further at our end. 
 
Regards, 
Berly B.C 



NA Nathanael March 10, 2021 02:39 PM UTC

Thank you so much for looking at this. Okay, I have tried with the code you generated in my own project and also just downloading the project that you sent me. Sure enough, when you look at LocalData in Code at the breakpoint, it is bringing 7 distinct games. However, when you go onto the webpage, the dropdown list still populates all the games including all three 'American Footballs'. 

I am trying to post a screenshot but I cant figure out how. But loading the project you sent, without changing anything, gave me that result. Where it still lists all three 'American Footballs'


BC Berly Christopher Syncfusion Team March 11, 2021 09:45 AM UTC

Hi Nathanael, 
 
We need to assign the filtered data source with help of distinct method again to the MultiSelect component’s data source property for reflect in the component popup. For your convenience, we have modified the sample and attached it below. Here, we have assigned the “LocalData” to the component’s data source property which is filtered by distinct method. 
 
<SfMultiSelect @ref="DropdownObj" TValue="string[]" TItem="string" Placeholder="Select a game" DataSource="@LocalData"> 
    <MultiSelectFieldSettings Value="ID" Text="Text"></MultiSelectFieldSettings> 
</SfMultiSelect> 
 
@code {  
    SfMultiSelect<string[], string> DropdownObj; 
    public class Games 
    { 
        public string ID { get; set; } 
        public string Text { get; set; } 
    } 
    List<Games> data { get; set; } 
    List<string> LocalData { get; set; } 
    protected override void OnInitialized() 
    { 
        data = new List<Games> { 
            new Games() { ID= "Game1", Text= "American Football" }, 
            new Games() { ID= "Game2", Text= "American Football" }, 
            new Games() { ID= "Game3", Text= "American Football" }, 
            new Games() { ID= "Game4", Text= "Cricket" }, 
            new Games() { ID= "Game5", Text= "Football" }, 
            new Games() { ID= "Game6", Text= "Golf" }, 
            new Games() { ID= "Game7", Text= "Badminton" }, 
            new Games() { ID= "Game8", Text= "Basketball"}, 
            new Games() { ID= "Game9", Text= "Snooker" }, 
            new Games() { ID= "Game10", Text= "Football"}, 
        }; 
        this.LocalData = data.Select(x => x.Text).Distinct().ToList(); 
    } 
 } 
 
 
Output Screenshot: 
 
 
Regards, 
Berly B.C 


Marked as answer
Loader.
Up arrow icon