Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Server Side Programming Articles
Page 830 of 2109
Clear a Hashtable in C#
The Clear() method in C# is used to remove all key-value pairs from a Hashtable. This method provides an efficient way to empty the entire Hashtable in a single operation, resetting its count to zero while maintaining the original capacity. Syntax Following is the syntax for the Clear()
Read MoreC# Program to get distinct element from a sequence
The Distinct() method in C# is used to remove duplicate elements from a sequence and return only unique values. This method is part of LINQ (Language Integrated Query) and works with any collection that implements IEnumerable. Syntax Following is the syntax for the Distinct() method − IEnumerable Distinct() IEnumerable Distinct(IEqualityComparer comparer) Parameters comparer (optional) − An IEqualityComparer to compare values for equality. If not provided, the default equality comparer is used. Return Value Returns an IEnumerable containing distinct elements from the source sequence. Using Distinct() with ...
Read MoreHow to handle empty collections in C#
Handling empty collections in C# is a common scenario that can lead to runtime errors if not managed properly. The DefaultIfEmpty() method from LINQ provides an elegant solution to work with empty collections by returning a default value when the collection contains no elements. When working with collections, you often need to ensure that operations don't fail on empty collections. The DefaultIfEmpty() method prevents exceptions and provides predictable behavior. Syntax Following is the syntax for using DefaultIfEmpty() method − collection.DefaultIfEmpty() collection.DefaultIfEmpty(defaultValue) Parameters defaultValue (optional) − The value to return if the ...
Read MoreGet first three letters from every string in C#
In C#, you can extract the first three letters from every string using the Substring() method combined with LINQ operations. This is useful for creating abbreviations, prefixes, or shortened versions of strings in a collection. Syntax Following is the syntax for getting a substring from a string − string.Substring(startIndex, length) Following is the syntax for applying substring to a collection using LINQ − collection.Select(str => str.Substring(0, 3)) Using Substring with LINQ Select The most common approach is to use the Substring() method with LINQ's Select() method to transform each ...
Read MoreAsEnumerable() in C#
The AsEnumerable() method in C# is used to cast a specific type to its IEnumerable equivalent. It is an extension method from the System.Linq namespace that helps when you need to treat a collection as a basic enumerable sequence, often to force LINQ to Objects behavior over LINQ to SQL or Entity Framework queries. Syntax Following is the syntax for using AsEnumerable() − public static IEnumerable AsEnumerable(this IEnumerable source) Usage example − var enumerable = collection.AsEnumerable(); Parameters source − The sequence to type as IEnumerable. Return ...
Read MoreC# Linq Intersect Method
The Intersect() method in C# LINQ is used to find common elements between two collections. It returns a new sequence containing elements that exist in both the source collection and the specified collection, with duplicates automatically removed. Syntax Following is the syntax for the Intersect() method − public static IEnumerable Intersect( this IEnumerable first, IEnumerable second ) With a custom equality comparer − public static IEnumerable Intersect( this IEnumerable first, IEnumerable second, ...
Read MoreC# Linq FirstorDefault Method
The FirstOrDefault() method in C# LINQ returns the first element of a sequence, or a default value if the sequence is empty or no element matches the specified condition. This method is useful when you need to safely retrieve the first element without throwing an exception. Syntax Following is the syntax for using FirstOrDefault() − // Without condition public static T FirstOrDefault(this IEnumerable source); // With condition public static T FirstOrDefault(this IEnumerable source, Func predicate); Parameters source − The sequence to return the first element from. predicate (optional) ...
Read MoreAsQueryable() in C#
The AsQueryable() method in C# is used to convert an IEnumerable collection into an IQueryable interface. This conversion enables LINQ query providers to translate queries into optimized database queries or other queryable data sources. The key difference is that IEnumerable executes queries in-memory using LINQ to Objects, while IQueryable can be translated into expression trees for remote execution, such as database queries. Syntax Following is the syntax for using AsQueryable() method − IQueryable queryable = collection.AsQueryable(); The method returns an IQueryable that represents the original collection as a queryable data source. Using ...
Read MoreC# Queryable Max Method
The Queryable.Max() method in C# returns the maximum value from a sequence of numeric values. It operates on IQueryable collections and provides query optimization for data sources like Entity Framework. Syntax Following is the syntax for the Max() method − public static TSource Max(this IQueryable source) public static TResult Max(this IQueryable source, Expression selector) Parameters source − An IQueryable sequence of values to determine the maximum value of. selector − A transform function to apply to each element (optional overload). Return Value Returns the maximum ...
Read MoreC# Queryable Min Method
The Min() method in C# is used to find the minimum value from a sequence. When used with AsQueryable(), it creates a queryable data source that can be efficiently processed using LINQ expressions. Syntax Following is the syntax for the Queryable Min() method − public static TSource Min(this IQueryable source); public static TResult Min(this IQueryable source, Expression selector); Parameters source: The IQueryable sequence to find the minimum value in. selector: A function to extract a value from each element (optional). Return Value Returns the minimum value in the sequence. ...
Read More