LINQ Method Syntax C# - How to write Method Syntax

In this chapter, you will learn: 
1. How to write Method Syntax in LINQ C#

In the previous chapter, you learned how to write LINQ Query Syntax. In this chapter, you will learn How to Write LINQ Method Syntax. LINQ Method Syntax uses the lambda expression to filter and display the result. In the next chapter, you will learn all about the lambda expression.

Programming Example

In this programming example, I have created a product list and used it as a data source and then display all the items using LINQ Method Syntax.

using System;
using System.Collections.Generic;
using System.Linq;

namespace LinqExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Creating List
            IList<string> productList = new List<string>()
            {
                "Hard Disk",
                "Monitor",
                "SSD Disk",
                "RAM",
                "Processor",
                "Bluetooth",
                "Keyboard & Mouse"
            };

            var result = productList.Where(p => p.Contains(""));           

            foreach(string str in result)
            {
                Console.WriteLine(str.ToString());
            }

            Console.ReadKey();
        }
    }
}

Output
Hard Disk
Monitor
SSD Disk
RAM
Processor
Bluetooth
Keyboard & Mouse

_

Example 2: Filtering Result

using System;
using System.Collections.Generic;
using System.Linq;

namespace LinqExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Creating List
            IList<string> productList = new List<string>()
            {
                "Hard Disk",
                "Monitor",
                "SSD Disk",
                "RAM",
                "Processor",
                "Bluetooth",
                "Keyboard & Mouse"
            };

            var result = productList.Where(p => p.Contains("Disk"));

            foreach(string str in result)
            {
                Console.WriteLine(str.ToString());
            }

            Console.ReadKey();
        }
    }
}
Output:
Hard Disk
SSD Disk
_

In this chapter, you leaned simple way to display data using LINQ Method Syntax. As this website claims "Less Theory Rich Programming", we are adding 20 LINQ Programming Example in next chapter that will give you quick understanding and hand on practice on LINQ. You must executes those programming example in your PC and very soon you will notice that you are very confident in LINQ. Later we will discuss the theories and other part of LINQ.

More Articles

 

Share your thought