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.
Please advise what I am doing wrong?
Below is my code:
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);
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);
public class Feed
{
public List<Career> Career { get; set; }
public List<Countries> countries { get; set; }
}
public class Career
{
public string Country { get; set; }
}
public class Countries
{
public string name;
}
ASKER
Yes... It gives me below error:
Error CS0266 Cannot implicitly convert type 'System.Collections.Generi c.IEnumera ble<string >' to 'System.Collections.Generi c.List<Res earch.Coun tries>'. An explicit conversion exists (are you missing a cast?)
Error CS0266 Cannot implicitly convert type 'System.Collections.Generi
You can do:
»bp
List<string> countries = new List<string>();
countries = career.Select(x => x.Country).Distinct().ToList();
»bp
ASKER
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
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
»bp
I don't think it's as simple as a cast...
»bp
»bp
Okay, this seems to work, not sure if there is a condensed approach...
»bp
List<Countries> countries = new List<Countries>();
countries = career.Select(x => x.Country).Distinct().Select(x => new Countries() { name = x }).ToList();
»bp
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Why is Career.Country a string instead of a Country?
ASKER
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.
@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.
Open in new window
»bp