Try the following
var list = originalList .GroupBy(x => x.Number) .Select(x => x.OrderBy(y => y.Percentage).First()) .ToList();
This query will first group the items by the
Number value. It will then order each group by the percentage and select out the first element (which is the lowest).
var query = (from l in lst group l by l.Number into g select g.Min(x => x.Percentage)).ToList();
Changing this up based on the interpretation that by "LINQ" the poster desires query-comprehension syntax instead of dot-syntax (otherwise the other responses suffice):
var query = from i in list group i by i.Number into gj select gj.OrderBy(x => x.Percentage).First();
But this still uses a lot of dot-syntax for the ordering. Which would be the functional equivalent of:
var query = from i in list group i by i.Number into gj select (from i in gj orderby i.Percentage select i).First();
If you don't like the
.First() call on the end, that's going to be a challenge that either will not have a solution within the LINQ language transforms or the solution you end up with will not be the clean solution that IMHO dot syntax provides for this problem. Query comprehensions can often make things simpler, but in this case I prefer the dot-syntax:
var query = list.GroupBy(i => i.Number) .Select(gj => gj.OrderBy(i => i.Percentage).First());
gj.OrderBy(...).First() portion accomplishes a More-LINQ-style MinBy operation (See Jon Skeet's post)
Would this work?
collection.GroupBy(item => item.Number).Select(group => group.Min())
var query = from x in list group x by x.Number into x select x.OrderBy(y => y.Percentage).First();
- C# - Select out the lowest of two values when duplicate key is found
- Duplicate key error but no entry with same values to be found when inserting data through LINQ
- Select only the lowest values with Linq
- Difference between the returned values of two selector functions in this code
- How can I split a List<T> into two lists, one containing all duplicate values and the other containing the remainder?
- Select all elements from dictionary when the key is contained in the second dictionary
- Linq: select a list by concatenating values from two (or more) fields of the same type
- Left join two tables with a default value when no match is found in the right table
- How to run linq query with select when a primary key column returns null and thus the related attribute is null
- In entity framework core, how we can set relationship for a table, when two fields is mapping to primary key of the another table
- Lookup elements in Hash where the key is composed of two values
- EF Query using .Contains() and .ToLowerInvariant() Results in no matches when match is found at the end of a string
- Comparing two Lists and returning the distinct values and the differences
- Test whether two IEnumerable<T> have the same values with the same frequencies
- Entity Framework - An item with the same key has already been added. - Error when trying to define foreign key relationship
- Select a section out of the middle of a list in C#
- How to compare the cell values of two data tables
- Combine the results of two columns in a select into one array with LINQ?
- linq query to join two tables and get the count from one table values from the other
- C# LINQ Select objects with the same value of one property join values of other
- select values tolist() when orderby distinct date
- Why do I get a "key already added" error when using the key from a GroupBy in ToDictionary?
- Is there a way to select two values into same IEnumerable using LINQ in C#?
- How can I make my Linq select return values if the value selected is null?
- Does Select followed by Where result in two iterations over the IEnumerable?
- Should we always use .Find() rather than .FirstOrDefault() when we have the primary key in Entity Framework Core?
- Effort (EF6) exception when accessing DbSet (The given key was not present in the dictionary)
- Removing a single item from an enumerable source when the items are equal
- How to select items from two lists where the ids don't match
- Avoid extra loop and could not find implementation of query pattern for source type int Select not found
More Query from same tag
- How to format a date/time being returned from database to only show the time segment
- Linq to Entities, Take(3) from Joined table
- LINQ to XML : A query body must end with a select clause or a group clause
- Cluster items by condition and order using linq
- LINQ Entity Framework query not working in EF Core, throwing exception
- How do i group a list of dynamic objects using linq given that the property names are dynamic?
- Client side GroupBy is not supported
- Occasional performance drops in linq queries to concurrent bag
- Printing objects from a list which is a value in a dictionary
- In LINQ, does orderby() execute the comparing function only once or execute it whenever needed?
- Microsoft.CSharp.RuntimeBinder.RuntimeBinderException Error
- Most Efficient way to test large number of strings against a large List<string>
- Subtract a generic list from another
- How to get the count of the values that are repeated in c#
- Query Using Entity Framework Error with Database Table name Error
- C# LINQ select from list of arrays of strings
- Linq : Select Different Object Left Join
- LinqPad Feature - Does VS do this?
- How can I optimize the following code faster?
- Change value of each element by linq
- LINQ to XML - Trying to select a list of elements by the value of their attributes
- Using linq to separate strings where the special character is
- C# - boolean property value wont change :(
- LINQ Left Join SQL equivalent via DefaultIfEmpty fails to return results
- How can get a result of a linq stored procedure into a label which is scalar
- Add values from a list of objects to another list of different objects
- predicate builder c# confusion
- Why doesn't EnumerableRowCollection<DataRow>.Select() compile like this?
- Query a Datagridview.Rows or Dataset using Linq
- Linq "Select" Sanity Question