---
title: "LINQ where() and OfType() using C#."  
description: "here are the explanation of where() and OfType() function in linq with C#"  
author: "Ashutosh Patel"  
published: 2025-02-12  
updated: 2025-02-12  
canonical: https://www.mindstick.com/articles/338505/linq-where-and-oftype-using-c-sharp  
category: "linq"  
tags: ["linq", "linq function"]  
reading_time: 2 minutes  

---

# LINQ where() and OfType() using C#.

In LINQ, Where and OfType are used to filter [collections](https://www.mindstick.com/articles/12458/what-are-collections-in-c-sharp), but they serve different purposes. Let's understand them with C# examples..

### Where() in LINQ

- [Filters](https://www.mindstick.com/forum/156637/use-jquery-filters-to-change-the-properties-of-an-element) [elements](https://yourviews.mindstick.com/story/1372/the-5-elements-of-nature) based on a condition.
- Works with any data type.
- Uses [lambda expressions](https://www.mindstick.com/forum/159874/what-are-lambda-expressions-and-how-are-they-used-in-linq-queries) to specify conditions.

## Example:

```cs
using System;
using System.Linq;
using System.Collections.Generic;
class Program
{
   static void Main()
   {
       List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
       // Get even numbers
       var evenNumbers = numbers.Where(n => n % 2 == 0).ToList();
       Console.WriteLine("Even Numbers: " + string.Join(", ", evenNumbers));
       // Output: Even Numbers: 2, 4, 6, 8
       List<string> names = new List<string> { "Alice", "Bob", "Andrew", "Charlie" };
       var aNames = names.Where(name => name.StartsWith("A")).ToList();
       Console.WriteLine("Names starting with A: " + string.Join(", ", aNames));
       // Output: Names starting with A: Alice, Andrew
   }
}
```

### OfType<T>() in LINQ

- Filters only elements of a specific type from a collection.
- [Useful](https://www.mindstick.com/articles/12694/how-dolomite-mineral-is-useful-in-human-life) when working with collections of mixed data types.

## Example:

```cs
using System;
using System.Linq;
using System.Collections.Generic;
class Program
{
   static void Main()
   {
       List<object> mixedList = new List<object> { 1, "Hello", 3.14, 5, "World", 10 };
       var integers = mixedList.OfType<int>().ToList();
       Console.WriteLine("Integers: " + string.Join(", ", integers));
       // Output: Integers: 1, 5, 10
       var strings = mixedList.OfType<string>().ToList();
       Console.WriteLine("Strings: " + string.Join(", ", strings));
       // Output: Strings: Hello, World
   }
}
```

## When to use What?

- Use Where() when [filtering](https://www.mindstick.com/forum/33958/how-to-filtering-with-the-entity-framework-is-an-asp-dot-net-mvc-application) based on conditions (for example, numbers greater than 5, [strings](https://www.mindstick.com/forum/161912/explain-the-python-strings) [starting](https://www.mindstick.com/blog/12024/things-you-need-to-know-when-starting-your-own-business) with "A").
- Use OfType<T>() when filtering based on data types in a mixed collection.

Also, Read: [Aggregate functions in LINQ](https://www.mindstick.com/articles/338503/explanation-of-all-linq-aggregate-functions)

---

Original Source: https://www.mindstick.com/articles/338505/linq-where-and-oftype-using-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
