Query a list and select top 10 values [duplicate] - c#

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);

Related

Running a Distinct on a record [duplicate]

This question already has answers here:
Distinct in Linq based on only one field of the table
(10 answers)
Closed 7 years ago.
In LINQ if the result that I have is the a list of whole records from the database and I want to do a Distinct on one column of this record, How do I do that?
You could try something as simple as:
var distincts = records.Select(x=>x.ColumnName).Distinct();

Get all combinatiion of Items of n number of lists [duplicate]

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();

Get all combinations of an array ( if i remove them 1 by 1 ) [duplicate]

This question already has answers here:
Algorithm to return all combinations of k elements from n
(77 answers)
What is the best way to find all combinations of items in an array?
(11 answers)
Closed 9 years ago.
Given:
{1,2,3}
Expected result:
{1,2,3},
{1,2},{1,3},{2,3},
{1},{2},{3}
So I want basically ALL possible combinations in a list ( but including all possible combinations of - when every element is removed ).
I hope you get what I mean ;)
Question: Which algorithm achieves this?
You want the power set algorithm.
There are some examples on Rosetta Code.

Get distinct parameters from a List [duplicate]

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).

How to get elements from collection A that not in collection B? i.e. A-B [duplicate]

This question already has an answer here:
Closed 13 years ago.
Possible Duplicate:
Disjoint Union in LINQ
DUPE: Disjoint Union in LINQ
I know this is a simple collection operation,My code is:
var gone = from a in A
where B.Contains(a) == false
select a;
but it not work.
var gone = A.Except(B);

Categories