How to Findout the Particuler Filed or Entity in c# list.

  By Ahsan   Posted on June-08-2023   72

C#

To find out which Filed/ Entity in a list match by  given Filed/Entity in C#, you can use LINQ to perform the search.

Here's an example:

==================C sharp==============
using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        List<string> names = new List<string>
        {
            "John",
            "Jane",
            "Bob",
            "Alice",
            "John"
        };

        string searchName = "John";

        // Find names that match the searchName
        List<string> matchingNames = names.Where(n => n == searchName).ToList();

        if (matchingNames.Count > 0)
        {
            Console.WriteLine("Matching names:");
            foreach (string name in matchingNames)
            {
                Console.WriteLine(name);
            }
        }
        else
        {
            Console.WriteLine("No matching names found.");
        }
    }
}

================================

In this code, we have a list of names stored in the `names` list. We use LINQ's `Where` method with a lambda expression `n => n == searchName` to find the names that match the `searchName`. The `ToList()` method is called to convert the filtered names to a new list. Finally, we check if any matching names were found and output them, or indicate that no matching names were found.

You can modify the code according to your specific requirements and the data structure you're using.

By      08-Jun-2023 Views  72



You may also read following recent Post

Submit Post - Article - Ask a Question - Ask an Interview Question - Submit Useful Links