.Any() - returns boolean.
The LINQ Any() method checks if at least one element in a collection satisfies a condition—or if the collection has any elements at all.
//----------------------------------------------
.All()
true if all elements match the condition. false if any one element fails the condition.
example
int[] numbers = { 2, 4, 6, 8 };
bool allEven = numbers.All(n => n % 2 == 0); // returns true
//------------------------------
use Count() without parameters to get the total number of elements, or pass a predicate (like n => n > 25) to count only those that match a condition.
example:
int[] numbers = { 10, 20, 30, 40, 50 }; // Count all elements int totalCount = numbers.Count(); // Count elements greater than 25 int countGreaterThan25 = numbers.Count(n => n > 25);
//--------------------------
The Contains() method checks whether a sequence contains a specified element and returns a boolean result.
example:
string[] fruits = { "apple", "banana", "mango", "orange" };
// Check if "mango" exists in the array
bool hasMango = fruits.Contains("mango");
//-------------------------
string[] names = { "Zara", "Adam", "John", "Bella" };
// Sort names in ascending order
var sortedNames = names.OrderBy(name => name);
OrderByDescending() if you want to sort in reverse order:
var descendingNames = names.OrderByDescending(name => name);
//---------------------------
Min
//---------------------------
Max
//---------------------------
Average
int[] numbers = { 10, 20, 30, 40, 50 };
double avg = numbers.Average();
//----
double averageMarks = students.Average(s => s.Marks);
//---------------------------
Sum
int[] numbers = { 10, 20, 30, 40, 50 };
// Using LINQ Sum method
int total = numbers.Sum();
//--- products.Sum(p=>p.Price);
//---------------------------
ElementAt(index) for IEnumerable<int>
to avoid exception use ElementAtOrDefault(index)
//---------------------------
FirstOrDefault()
LastOrDefault()
//----------------------------
Single()
//----
SingleOrDefault()
This method returns the only element that matches a condition, or the default value (null for reference types) if no such element exists. It throws an exception if more than one match is found.
var users = new List { "Alice", "Bob", "Charlie" }; // This will return "Bob" string result = users.SingleOrDefault(u => u == "Bob");
//---------------------------
Where
//---------------------------
Take(int), TakeLast(int)
//--
TakeWhile()
int[] numbers = { 10, 20, 30, 60, 40, 50 };
var result = numbers.TakeWhile(n => n < 50);
//-----------------------------
Skip(int), SkipLast(int), SkipWhile(int)
numbers.Skip(2).Take(2)
numbers.SkipWhile(number => number < 20)
//------------------------------
OfType<T>
ArrayList items = new ArrayList();
items.Add("hello"); items.Add(42); items.Add("world"); items.Add(3.14);
// Filter only strings
var strings = items.OfType<string>();
//------------------------------
Distinct()
List<int> numbers = new List<int>() { 1, 2, 3, 2, 4, 3, 5 };
var distinctNumbers = numbers.Distinct();
O/P: { 1,2,3,4,5 }
SyntaxEditor Code Snippet
public static IEnumerable<T> GetCollectionWithMostDuplicates<T>(IEnumerable<IEnumerable<T>> collections)
{
return collections.OrderBy
(collection => collection.Count() - collection.Distinct().Count())
.ThenByDescending(collection=>collection.Count())
.LastOrDefault();
}
//------------------------------
Append() & Prepend()
//---------------------------
Concat() & Union()
example
numbers1.Concat(numbers2)
num1.Union(num2) -- remove duplicates
var unionofpets = pets1.Union(pets, PetEqualityByIdComparer);// - union by two reference collection
//------------------------------------------------