LINQ

LINQ Where Clause Filtering Operator

Linq filtering operators are used to filter the collection or list. Filtering operator specifies the statement that should only take effect when the specified criteria meet. The where clause is optional and used to limit the number of data returned by the linq query.

Linq Where Operator with Method Syntax

Syntax:

collection name.Where(x=>x.StartWith("F"))

Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LinqDemoes
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] state = { "Surat", "Ahmedabad", "Rajkot", "Gandhinagar", "Saurashtra", "Anand" };
            IEnumerable<string> response = state.Where(x => x.StartsWith("A"));
            foreach (var country in response)
            {
                Console.WriteLine(country);
            }
            Console.ReadLine();
        }
    }
}

We are using StartsWith() method for matching the string content. In standard SQL we were using like operator for this. Also others like Contains(), etc. can be used with this and many more.

Output:

Ahmedabad
Anand

Linq Where Operator Query Syntax

Syntax

IEnumerable<string> response = from x in state where x.StartsWith("A") select x;

Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LinqDemoes
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] state = { "Surat", "Ahmedabad", "Rajkot", "Gandhinagar", "Saurashtra", "Anand" };
            IEnumerable<string> response = from x in state
                                         where x.StartsWith("A")
                                            select x;
            foreach (var country in response)
            {
                Console.WriteLine(country);
            }
            Console.ReadLine();
        }
    }
}

Output:

Ahmedabad
Anand
Faisal Pathan

Faisal Pathan is a founder of TheCodeHubs, .NET Project Manager/Team Leader, and C# Corner MVP. He has extensive experience with designing and developing enterprise-scale applications. He has good skills in ASP.NET C#, ASP.NET Core, ASP.NET MVC, AngularJS, Angular, React, NodeJS, Amazon S3, Web API, EPPlus, Amazon MWS, eBay Integration, SQL, Entity Framework, JavaScript, eCommerce Integration like Walmart, Tanga, Newegg, Group-on Store, etc. and Windows services.

Share
Published by
Faisal Pathan

Recent Posts

Testing hk

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Operation

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

TETS NEW

test

3 years ago