score:-1
The Count()
method takes a predicate, so you would do something like this:
if(myObjectList.Count(x => x.Order == 1) >= 2)
{
// Do something
}
score:11
score:1
Not a pure Linq-To-Objects-Solution, but how about:
var ordersList = new List<Order>(myObjectList.Select(obj => obj.Order);
bool allUnique = ordersList.Count == new HashSet<Order>(ordersList).Count;
One would have to test performance of all the approaches presented here. I'd be careful, otherwise you end up quickly with some slow O(n²) look-ups.
score:2
bool hasDuplicates = myObjectList.Count >
new HashSet<int>(myObjectList.Select(x => x.Order)).Count;
score:1
What about Distinct
?
bool allUnique = ordersList.Count == ordersList.Select(o => o.Order).Distinct().Count()
Source: stackoverflow.com
Related Articles
- Determine whether two or more objects in a list are equal according to some property
- Copy a list of objects to another typed list in one line of code
- Find matching IDs between two different List Objects only when all IDs or more are present?
- Cannot get more than one result when using System.Linq.Dynamic.Core to query a list of objects that have a member dictionary
- Check all properties in list of objects is equal
- selecting some specific xml elements to list of anonymous objects
- LINQ query for removing all objects with an id not equal to a list of ids now workign properly
- Get list of items where their ID is equal to some Values - linq c#
- How to verify whether all dictionary KVP are included in some of other dictionary within list
- How to loop through a child list of objects in Entity Framework 4.1 code first
- Linq query for updating list of objects with some data from another list of objects
- Code Optimization to check some items of checkbox list
- Compare two list of objects and select common and difference records based some property
- How to bind and save an object containing a list of objects to database context in ASP.NET MVC with EF code first?
- Find duplicate objects in list when grouping on one or more fields
- LINQ query to find objects in list with equal values for one of their properties
- Checking if a property in a list of objects is equal for all items
- LINQ check list of objects contain a value AND not some other values
- List or Array of String Contain specific word in Html Source Code
- How to merge more than one list having objects into one list with sum of one of the elements of the list in c#
- How to align a list of objects according to their string properties?
- Query to determine whether more than one element has the same value for a property?
- c# Linq or code to extract groups from a single list of source data
- Create a new list in C# with LINQ, sorted by type and ignoring objects of the same type if they occur more than once
- Checking whether a property value equals to a predefined one in a list of objects
- Sorting a list using Lambda/Linq to objects
- Searching if value exists in a list of objects using Linq
- Linq select objects in list where exists IN (A,B,C)
- Using Linq to group a list of objects into a new grouped list of list of objects
- Convert a list of objects to an array of one of the object's properties
- SQL conversion to LINQ - efficiency
- Get value in foreach-loop
- Invoking OrderBy (System.Linq.Enumerable) with Reflection
- What is LINQ Actually Compiled To?
- What means a combined All with Any LINQ query
- Range variable hides a variable in an enclosing block LINQ
- LINQ Let operator with lambda syntax
- How to copy a row range from one column to others in a column oriented c# datatable?
- How to get row count 3 table join and lambda expressions?
- Why implement IEqualityComparer<T> in a separate class
- Non-static method requires a target. cant find an answer that seems to work
- Return Entity Framework objects over WCF
- Extract filtered list from large list
- Cannot cast from generic to get LINQ where clause
- Unable to create a constant value of type 'Anonymous type'. Only primitive types or enumeration types are supported in this context two db Linq query
- Usinq Linq to search delimited list with another delimited list
- How to retrieve the Attribute Values into a class using LINQ?
- Get Unique rows using Entity framework
- Descendants within descendants in LINQ to XML
- Getting Keys and Values from IEnumerable<Dictionary<string, object>>