score:31
you can check by using all
method, presumably your list have students:
var firststudent = students.first();
students.all(s => s.age == firststudent.age);
score:0
if you can't use linq, you can always loop all the students:
private bool sameage (list<student> students)
{
int auxage = students.count > 0? students[0].age : 0;
foreach (student stu in students)
{
if (stu.age != auxage)
return false;
}
return true;
}
score:0
if you just have to check this once, the marked answere is best solution. to use it multiple times in your code just write a static extension to check equality of property:
public static bool getidenticproperty<tsource, tkey>(this ienumerable<tsource> source, func<tsource, tkey> predicate)
{
ienumerable<tsource> enumerable = source as ilist<tsource> ?? source.tolist();
if ((!enumerable.any()) || (enumerable.count() == 1))
return true; // or false if you prefere
var firstitem = enumerable.first();
bool allsame = enumerable.all(i => equals(predicate(i), predicate(firstitem)));
return allsame;
}
if you want to use the value of the property later on lets return that value:
public static tkey getidenticproperty<tsource, tkey>(this ienumerable<tsource> source, func<tsource, tkey> predicate)
{
ienumerable<tsource> enumerable = source as ilist<tsource> ?? source.tolist();
if (!enumerable.any())
return default(tkey);
var firstitem = enumerable.first();
bool allsame = enumerable.all(i => equals(predicate(i), predicate(firstitem)));
return allsame ? predicate(firstitem) : default(tkey);
}
but using this code you have to check weather the returnd value is null
or default(tkey)
relating to the type of property.
score:3
if students can have 0 elements you can do this:
var firststudent = students.firstordefault();
var aresame =students.all(s => s.age == firststudent.age);
score:4
just a random answer - not sure i'd do it this way in reality, but this will be brutally efficient:
- will use typed iterators if duck-typing is available (unlike linq which won't) - and note that
list<t>
does offer duck-typed iterators - no double-iterations
- no failures etc for empty sequences
- no delegates, capture-contexts, etc
- etc
code:
using(var iter = students.getenumerator()) // a list<t>.enumerator struct
{
if(!iter.movenext()) return true; // or false, as you define for "empty"
int age = iter.current.age;
while(iter.movenext())
if(iter.current.age != age)
return false;
return true;
}
score:11
if you want to do this in one query, not two (which is generally bad practice),
bool allagesarethesame = (students.select(s => s.age).distinct().count() < 2);
will do it for you.
this will also return true in the trivial case where you have no students at all, rather than throw an exception. (you could do == 1
rather than < 2
to return false in the trivial case instead.)
Source: stackoverflow.com
Related Query
- how to check if property value of each list member is same
- How to check if all list items have the same value and return it, or return an “otherValue” if they don’t?
- LINQ to check each property of a list object to see if it's value is equal to a string
- How to check list A contains any value from list B?
- How to get/find an object by property value in a list
- How to check if all of the elements in a list return true for a property using Linq?
- LINQy way to check if any objects in a collection have the same property value
- How to find the maximum value for each key in a List of Dictionaries using LINQ?
- How to find how many times the same object appears in a list and then find a property of the most appeared object
- How to find same classes in a list with one different value
- How to filter a list with linq depends on counting a property at the same list and take a random group at least minimum total
- How to select all objects that have a property value in list of values?
- Using ASP.NET and MVC 3, how can I create hidden fields so that a List with an array as a value of each item in the list binds correctly?
- C# how to group List by objects with the same property values
- How can I create list of nested classes where a certain property of each nested class is true?
- How to select all objects sharing a property value with a property value in list of objects?
- How to determine if all objects inside List<T> has the same property value using Linq
- Using list extensions how to check if value exists and I am not out of range?
- How to check string for null and assign its value in the same line
- Using Linq to object, how can I get one field's value based on another in the same list
- How can you filter list by property value in an int list?
- How to check for the absence of any value in a list from a set server-side in LINQ? (SQL given)
- How to find all items in a list where a property has a specific value or where there is a related item with this value?
- Linq extension. Change property value in source list
- How to compare two lists and set value to list property
- LINQy way to check if any objects in a more than one collection have the same property value
- Getting all items in a list that have a property set to the same value
- How to get the property from a list of object that is a string with the lowest numeric value
- Order a list by a string value with the same name as the property
- How to take the Max value from a List of object where the same objects exists with many duplicate rows
More Query from same tag
- Summing up property values using LinQ
- SQL Server database shared among different projects
- Using LINQ to return a list of entities that match all values in a list passed in as a parameter
- Entity Framework - dynamic sql
- Is there a way to set this entity/linq query to IEnumerable?
- Converting complex SQL with FULL JOIN to Linq
- Writing an anonymous method for a method in IEnumerable
- Group By struct list on multiple columns in C#
- XML elements to Tuple using LINQ?
- Comparing Lists under multiple conditions
- insert data into two different tables using Linq
- Compare dates linq c#
- c# syntax and Linq, IQueryable
- SelectMany to flatten a nested structure
- Is it possible to select items from IEnumerablle by two ids?
- LINQ Query across Associations
- How to convert and assign result of a linq query to a datatable?
- how to get duplicate items from a list in vb.net
- Dynamic where condition in linq query expression
- How to group by in linq in c#?
- LINQ asp.net page against MS Access .
- Entity Framework: What is use /Meaning of (?) question mark
- Linq's FirstOrDefault to act as other type?
- SQL statement converting to LINQ?
- Entity Framework 6 context not retrieving navigation properties
- Linq query using list or array of ids
- Groovy 1.8 :: LINQ Applied
- Union 2 lists with additional content change for ID that occurs in both source lists
- In MVC3 how to get a list of models with Linq?
- Linq EF Include() with Select() new type lost included