---
title: "How to use a lambda expression in Enumerable.Distinct()?"  
description: "How to use a lambda expression in Enumerable.Distinct()?"  
author: "Steilla Mitchel"  
published: 2023-08-18  
updated: 2023-08-19  
canonical: https://www.mindstick.com/forum/159569/how-to-use-a-lambda-expression-in-enumerable-distinct  
category: "c#"  
tags: ["c#", "lambda expression"]  
reading_time: 2 minutes  

---

# How to use a lambda expression in Enumerable.Distinct()?

How to use a [lambda expression](https://www.mindstick.com/forum/159550/how-to-implement-an-interface-with-two-abstract-methods-by-a-lambda-expression) in Enumerable.Distinct()?

## Replies

### Reply by Aryan Kumar

Sure, I can help you with that.

To use a [lambda](https://www.mindstick.com/blog/181/lambda-expression-in-c-sharp) [expression](https://www.mindstick.com/articles/1861/sqlite-expressions) in `Enumerable.Distinct()`, you need to pass the lambda expression as the predicate parameter to the `Distinct()` method. The predicate parameter is a function that takes two elements of the sequence as its parameters and returns a boolean value. The `Distinct()` method will return a sequence that contains only the elements of the original sequence for which the predicate returns `true`.

Here is an example of how to use a lambda expression in `Enumerable.Distinct()`:

C#

```plaintext
using System;
using System.Collections.Generic;

namespace DistinctLambdaExpression
{
    public class Program
    {
        public static void Main(string[] args)
        {
            // Create a list of numbers.
            List<int> numbers = new List<int>();
            numbers.Add(1);
            numbers.Add(2);
            numbers.Add(3);
            numbers.Add(1);
            numbers.Add(2);

            // Use a lambda expression to remove duplicate numbers.
            List<int> distinctNumbers = numbers.Distinct((x, y) => x != y);

            // Print the distinct numbers.
            foreach (int number in distinctNumbers)
            {
                Console.WriteLine(number);
            }
        }
    }
}
```

In this example, the lambda expression takes two integers as its parameters and returns a boolean value. The lambda expression returns `true` if the two integers are not equal, and `false` if the two integers are equal. The `Distinct()` method will return a sequence that contains only the elements of the original sequence for which the predicate returns `true`.


---

Original Source: https://www.mindstick.com/forum/159569/how-to-use-a-lambda-expression-in-enumerable-distinct

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
