score:344
Accepted answer
ienumerable<int> ids = list.select(x=>x.id).distinct();
score:2
int[] numbers = {1,2,3,4,5,3,6,4,7,8,9,1,0 };
var nonrepeats = (from n in numbers select n).distinct();
foreach (var d in nonrepeats)
{
response.write(d);
}
output
1234567890
score:2
when taking distinct, we have to cast into ienumerable too. if the list is <t> model, it means you need to write code like this:
ienumerable<t> ids = list.select(x => x).distinct();
score:13
using straight linq, with the distinct()
extension:
var idlist = (from x in yourlist select x.id).distinct();
score:30
use the distinct operator:
var idlist = yourlist.select(x=> x.id).distinct();
Source: stackoverflow.com
Related Query
- How do I use LINQ to obtain a unique list of properties from a list of objects?
- How can you use LINQ to cast from a collection of base class objects to a filtered list of their respective subclasses?
- How can I obtain objects from list with C# LINQ using boolean conditions?
- How to use LINQ to query from a list of objects into an existing object?
- How to use Linq to check a list of objects property have allowed values from another list
- How to use LINQ to get an object from List of objects matching option from List of options
- Can I use LINQ to check if objects in a list have a unique ID?
- Can I use LINQ to create a new list of objects from an existing list
- How to generate a unique list of items from another list using LINQ in C#
- How to use LINQ to query list of strings that do not contain substring entries from another list
- How to use Linq to select and group complex child object from a parents list
- How to get number of objects with common properties from context using LINQ
- How to use LINQ to get unique columns from a DataTable
- How can I use LINQ to filter a collection of nested classes to yield a dictionary of unique properties of those classes?
- How to get all Objects from a List in a List with LINQ
- How to gather multiple int properties from a list of objects in a list of integers using lambda or linq?
- c# - Linq select properties from List of objects and modify them with shorthand conditional
- how to remove objects from list by multiple variables using linq
- How do I use LINQ to .Select from a List inside a Map?
- How to use LINQ to take x rows from a multi column list
- LINQ to Objects: How to join different properties in the same List of objects
- How do I remove objects with a combination of duplicate of two properties from List with linq?
- How to sort list on multiple properties in one line of code in Linq
- How to use LINQ to retrieve a list from another entity with a specific number of items
- how to use linq to retrieve values from a 2 dimensional generic list
- How to change values of few properties from one list to another using LINQ C#:
- How do I use Linq with a HashSet of Integers to pull multiple items from a list of Objects?
- How to use LINQ to iterate and display a list of objects in groups
- How can I use linq to search inside a list of objects
- How extract properties from several objects with LINQ
More Query from same tag
- Delete records based on array list - LINQ
- Join collection inside an Entity LINQ statement
- Linq Join using bit mask logic
- How to sort a list of Classes
- Failed to read attributes from XML file
- Exclude dates with LINQ
- Convert SQL query (with outer join and datediff) to LINQ
- Changing HQL to Linq?
- LINQ Group Rows Into Columns
- Byte array empty check in LINQ
- Select two columns from the table via linq
- Nothing gets added to Object list
- Cached results in Linq Query
- Why ordinary laws in evaluating boolean expression does not fit into LINQ?
- Merge two dynamic lists and override values
- Querying a collection of collection using Linq
- get a list of attributes from an element
- Make a Collection from Collection of Collection
- How to iterate only distinct string values by custom substring equality
- How to simulate .Substring() method in Linq-to-Entities
- How to replace the Text value with another string inside SelectListItem or SelectList mvc3?
- linq sql where closest to number
- How to load two SQL Tables into dataset and how to bind it to ListView?
- LINQ to entity, sort the subset of returned values
- Strange Exception thrown using Dynamic Linq Entity Framework Query
- EF Core (LINQ) - The Query expression could not be Translated
- Updating a single column in LINQ to SQL
- WPF Datagrid has data in the rows but not displaying text
- Execute multiple queries then union them with LINQ?
- LINQ to Entities group by and Count()