This question already has answers here:
Get distinct items from a list
(4 answers)
Linq Distinct() by name for populate a dropdown list with name and value
(7 answers)
LINQ: Distinct values
(8 answers)
Closed 9 years ago.
I have a CarData object with the following properties:
PrimaryKey Make Model Year Drivetrain Country
I have about 1000 of these CarData objects in a List :
List<CarData> CarObjects
Is there a simple way to get a list of the distinct Makes?
var makes = CarObjects.Select(car => car.Make).Distinct();
This transforms the list from a list of CarData to a list of Makes, and then just finds the distinct values of the new list.
You can use Linq:
CarObjects.Select ( c => c.Make ).Distinct().ToList()
var makeList = CarObjects.Select(a => a.Make).Distinct();
Or
List<MakeEnum> = CarObjects.Select(a => a.Make).Distinct().ToList();
As an extra bit of advice, you may want to consider having Make be an enum, since there are (presumably) a finite (and rather small) number of possible makes of cars, instead of piling them into Strings. (You don't make a mention of what kind of property Make is, so maybe you are already doing this).
Related
This question already has answers here:
Linq query and casting in c#
(2 answers)
Closed 10 months ago.
I have a Class, Food which has two derived classes: Meat and Vegetables.
If I have a List of Foods, I can get a (sub)list of Foods that are Meat using
List<Food> allTheFood = GetListOfFood();
List<Food> justTheMeats = allTheFood.Where(x => x is Meat);
But this gives me a list of type Food (where they all happen to be Meat), rather than a list of Meat.
Obviously, I could create a new empty List and then do a foreach and cast every individual item from justTheMeats from Food to Meat and add them into the new List, but that seems a convoluted approach.
Is there a nicer/easier/cleaner way to do this?
Thanks
Yes, use linq OfType
var justTheMeats = allTheFood.OfType<Meat>().ToList();
This question already has answers here:
How to get first N elements of a list in C#?
(7 answers)
Closed 4 years ago.
I have a list of objects in which each object has a property called "Frequency" and I want to be able to pick the top 10 objects that have the highest frequencies.
I saw some solutions that are kind of similar to what I am looking to solve using LINQ so any help is appreciated.
You can order the list by descending Frequency and then take the first 10 like this:
var top10 = objectList.OrderByDescending(o => o.Frequency).Take(10);
This question already has answers here:
How to perform .Max() on a property of all objects in a collection and return the object with maximum value [duplicate]
(9 answers)
Closed 5 years ago.
What I want to do is basically
var max = things.Select(t => ExpensiveFunc(t)).Max();
var ThingWithMaxResult = things.Where(t => ExpensiveFunc(t) == max).First();
But I don't want to have to run the ExpensiveFunc twice on each element.
I am learning LINQ so I would like to know the LINQ way of doing this. Otherwise I would normally create an array of things and results, then just pick the array with the highest result.
You can order by the function call (descending) and take the first one:
var max = things.OrderByDescending(ExpensiveFunc).First();
This question already has answers here:
LINQ's Distinct() on a particular property
(23 answers)
Closed 8 years ago.
Distinct() is not working. It displays all the repeating values.
I searched for a solution but just got more confused. I tried this :
var categories = db.Orders.OrderBy(c => c.Item1).ToList().Distinct();
var categories = db.Orders.Distinct().OrderBy(c => c.Item1).ToList();
Is there a quick uncomplicated way to make this work?
use GroupBy instead of Distinct
This question already has answers here:
Is there a good LINQ way to do a cartesian product?
(3 answers)
How can I compute a Cartesian product iteratively?
(4 answers)
Closed 8 years ago.
I have a list that contains further sub lists and i have objects stored in the sub list.
I want to generate all possible combinations of all elements.
e.g. we have a list that contains two list L1,L2 and all have different objects stored in the for example L1 contains {obj1,obj2}
L2 contains {obj3,obj4}
then the result should come in the form of
{obj1,obj3}
{obj1,obj4}
{obj2,obj3}
{obj2,obj4}
all lists are being generated dynamically. so the solution should be generic irrespective of count of elements in main list and sub list
L1.SelectMany(l1 => L2.Select(l2 => Tuple.Create(l1, l2))).ToList();