score:1
Here is the right answer (to be compared with Alex Aza's one : filteredItemsAlexAza and filteredItemsSsithra give different results since the less common data is not also the minimum one anymore - here 2005 instead of 2001 for A01)
class Program
{
public static void Main(string[] args)
{
var items = new List<Item>
{
new Item { Code = "A01", Year = "2005" },
new Item { Code = "A01", Year = "2002" },
new Item { Code = "B01", Year = "2002" },
new Item { Code = "C01", Year = "2003" },
};
var filteredItemsAlexAza = items.Select(cod => cod.Code).Distinct().Select(cod => items.OrderBy(itm => itm.Year).First(itm => itm.Code == cod)).ToList();
var filteredItemsSsithra = items
.Select(item => new { Item = item, NbItemsWithSameYear = items.Where(i => i.Year == item.Year).Count() })
.GroupBy(ano => ano.Item.Code)
.Select(group => group.OrderBy(ano => ano.NbItemsWithSameYear).First().Item)
.ToList();
}
public class Item
{
public string Code { get; set; }
public string Year { get; set; }
}
}
score:0
You can google for "Concordance source code" in your preferred implementation language.
score:2
I'm sorry for making it in C#. Hope you will not have problems to convert it to VB.NET.
var filteredItems = items
.Select(cod => cod.Code).Distinct()
.Select(cod => items.OrderBy(itm => itm.Year).First(itm => itm.Code == cod))
.ToList();
Test code:
public class Item
{
public string Code { get; set; }
public string Year { get; set; }
}
public static void Main(string[] args)
{
var items =
new List<Item>
{
new Item{ Code = "A01", Year = "2001" },
new Item{ Code = "A01", Year = "2002" },
new Item{ Code = "B01", Year = "2002" },
new Item{ Code = "C01", Year = "2003" },
};
var filteredItems = items
.Select(cod => cod.Code).Distinct()
.Select(cod => items.OrderBy(itm => itm.Year).First(itm => itm.Code == cod))
.ToList();
}
score:0
Sorry but the proposed solution absolutely doesn't fullfill Pileggi's initial requirements. "The less common value as possible" has become "the min value". Both match in this precise case, which gives the illusion that Alex Aza's answer is right but it is just a coincidence.
Source: stackoverflow.com
Related Articles
- Get the data 'less common as possible' from a collection
- c# Linq or code to extract groups from a single list of source data
- How does linq actually execute the code to retrieve data from the data source?
- Easiest way to get a common base class from a collection of types
- LINQ - C# - Using lambda - Get a set of data from a Collection
- The given value of type String from the data source cannot be converted to type int of the specified target column
- Most efficient collection for storing data from LINQ to Entities?
- creating Linq to sqlite dbml from DbLinq source code
- Get the Common data from the List
- C# LINQ How to get a data source from a db?
- Speeding up lookup of ranges of data from a collection
- C# SQL/Linq/Entity Framework calculating column totals for multiple columns from large data source
- How to retrieve image data from a dynamic module collection in Sitefinity 10?
- Using Linq to pass data from one collection to another
- How do I group items from a collection using LINQ and return the shaped data according the collection type
- get selected value from combobox with data source is anonymous type
- Is It Possible To Dispose a LinQtoSQL Object from the Dataclass to Counter Data Concurrency?
- How to reinsert data from one table onto itself using LINQ in code migration?
- Select data from anonymous collection
- Filter data from a collection
- How to retrieve data from multiple tables that may or may not have values in common
- How to create one collection by combining data from two collections?
- C# LINQ or for loop How to get a data source from a db?
- how to fetch data from database using linq query for relationship 1:N and N:N (between 3 entity) in asp.net mvc EF code first?
- Linq query to get data from a specific RavenDB collection not working as expected
- Extracting data from a collection in Java
- Find a certain value from a source collection in a target collection of different type with linq
- Get most common data from CSV File with LINQ
- Join 2 collection list together from 2 data sources find the matches and loop through the results
- Loading data from different collection based on parameter
- System.FormatException: String was not recognized as a valid DateTime
- How to use query datetimes with linq?
- Linq complex queries C#
- How to use a string variable in the linq where clause?
- CS1929 - List<T> does not contain an implementation of Where() (using System.Linq)
- Funky LINQ Required
- How to split a List with different chunk sizes efficiently?
- LINQ query not working - C#
- Correctly Translate .Take(1)(0) to C# to fix Cannot apply indexing with [] error?
- Linq Query with using sum and order by
- Enhance this LINQ query for readability and performance?
- How to Connect to SQL Server using LINQ to SQL?
- Is it possible to have an anonymous method when creating a list of items, with .NET + Linq?
- Search for null objects inside array of objects using LINQ
- Structuring models in a simple hierarchy in MVC, what's the best practice?
- How do you handle arbitrary namespaces when querying over Linq to XML?
- Unable to cast object of type 'System.Data.Entity.Infrastructure.DbQuery`1[]' using linq lambda expression
- Linq - Set int to 0 if it is a minus number
- How can I automatically .Trim() whitespace from my Linq queries?
- How to get items that were effective within a date range in linq