Link to home
Start Free TrialLog in
Avatar of Dinesh Bali
Dinesh Bali

asked on

get Distinct value from List using Linq

Hi,
I am working on C#.
I have list and I wanted to get distinct countries.

// Find distinct countries and 
            countries = career.Select(x => x.Country);

Open in new window


Please advise what I am doing wrong?

Below is my code:

 
Feed feed = new Feed();
            List<Career> career = new List<Career>();

            Career careerOne = new Career();
            careerOne.Country = "Australia";
            career.Add(careerOne);

            Career careerTwo = new Career();
            careerTwo.Country = "UK";
            career.Add(careerTwo);

            Career careerThree = new Career();
            careerTwo.Country = "Australia";
            career.Add(careerThree);

            List<Countries> countries = new List<Countries>();

            // Find distinct countries and 
            countries = career.Select(x => x.Country);

Open in new window


public class Feed
    {
        public List<Career> Career { get; set; }
        public List<Countries> countries { get; set; }
    }

Open in new window


public class Career
    {
        public string Country { get; set; }
    }

Open in new window


public class Countries
    {
        public string name;
    }

Open in new window

Avatar of Bill Prew
Bill Prew

Have you tried:

countries = career.Select(x => x.Country).Distinct();

Open in new window


»bp
Avatar of Dinesh Bali

ASKER

Yes... It gives me below error:

Error      CS0266      Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<string>' to 'System.Collections.Generic.List<Research.Countries>'. An explicit conversion exists (are you missing a cast?)
You can do:

            List<string> countries = new List<string>();
            countries = career.Select(x => x.Country).Distinct().ToList();

Open in new window



»bp
Thanks for this.
But I need list of countries object only List<Countries> countries

As I need to use other details as well.

I have not exposed all variables as this might confuse.

Above solution will give me list of string List<string>

Please advise for countries list List<Countries> countries
Well, in your example you are retrieving a list of career.Country which is of type string.  If you want to convert that to Countries then you will need to cast it, which should work okay since they are both string in your example.  Although I would have to experiment a bit to get that right I think...


»bp
I don't think it's as simple as a cast...


»bp
Okay, this seems to work, not sure if there is a condensed approach...

            List<Countries> countries = new List<Countries>();
            countries = career.Select(x => x.Country).Distinct().Select(x => new Countries() { name = x }).ToList();

Open in new window


»bp
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Why is Career.Country a string instead of a Country?
Many thanks all.

@kaufmed,
The data on career is coming from CMS. So model is not created by my application.
Hence that was string and not countries object.

Thanks again.