Csharp Articles

Page 155 of 196

How to handle empty collections in C#

Chandu yadav
Chandu yadav
Updated on 17-Mar-2026 487 Views

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 More

Get first three letters from every string in C#

George John
George John
Updated on 17-Mar-2026 3K+ Views

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 More

AsEnumerable() in C#

Samual Sam
Samual Sam
Updated on 17-Mar-2026 5K+ Views

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 More

C# Linq Intersect Method

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 695 Views

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 More

C# Linq FirstorDefault Method

Ankith Reddy
Ankith Reddy
Updated on 17-Mar-2026 20K+ Views

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 More

AsQueryable() in C#

Arjun Thakur
Arjun Thakur
Updated on 17-Mar-2026 4K+ Views

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 More

C# Queryable Max Method

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 255 Views

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 More

C# Queryable Min Method

Ankith Reddy
Ankith Reddy
Updated on 17-Mar-2026 199 Views

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

Represent Int32 as a Octal String in C#

George John
George John
Updated on 17-Mar-2026 1K+ Views

To represent Int32 as an octal string in C#, use the Convert.ToString() method with base 8 as the second parameter. An Int32 represents a 32-bit signed integer that can hold values from -2, 147, 483, 648 to 2, 147, 483, 647. Syntax Following is the syntax for converting an Int32 to octal string − Convert.ToString(intValue, 8) Parameters intValue − The integer value to convert. 8 − The base for octal representation. Return Value The method returns a string representing the integer in octal format (base ...

Read More

C# Orderby Descending

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 442 Views

The orderby descending clause in C# is used with LINQ to sort collections in descending order. It works with both query syntax and method syntax, allowing you to sort objects by any property or field. Syntax Following is the syntax for using orderby descending with LINQ query syntax − var result = from item in collection orderby item.Property descending select item; Following is the syntax for ...

Read More
Showing 1541–1550 of 1,951 articles
« Prev 1 153 154 155 156 157 196 Next »
Advertisements