LINQ In Python For C#

Introduction 

 
LINQ (Language Integrated Query) is a query language to deal with the query operators to perform the operations with the collections.
 
In Python, we are able to achieve LINQ methods like C#. In this blog, we will look at C# equivalent LINQ methods in Python. We can achieve LINQ, using most of Python's built-in function and iterators.
 
In Python, we define a list of flowers as a collection
  1. Colours = [‘Red, ’Green’, ’Blue’, ’Black’, ’White’, ’Orange’, ’Yellow’]   
In Python, built-in functions are the same as in LINQ. I will give some samples for Python built-in functions and iterators; i.e., All & Any
 
Any method, the command is given below.
  1. any (“a” in c for c in Colours)   
This method will check any color which contains the ‘a’ letter in it and the result for this query will return true.
 
For All Method, the command is given below.
  1. all (“a” in c for c in Colours)   
This method will check all the colors, which contain the ‘a’ letter and the result for this query is false.
 
Min & Max
 
For the Min method, the command is given below.
  1. min (len(a) for c in Colours)   
This method will check the minimum length of the color and the result is 3.
 
For Max Method, the command is given below.
  1. max (len(a) for c in Colours)   
This method will check the maximum length of the color, and the result is returned as  6.
 
Take & Skip
 
In Python, we need to use an itertools module to include an islice built-in function. To access it, we need to call itertools in our page, using the method given below.
  1. from itertools import islice   
Here, islice function is used to achieve the functions; i.e., take (2) and skip (2), as shown below in Python,
 
For take function, the command is given below
  1. list(islice(Colours ,2))   
This method will take 2 colors from the collection and return the output, as shown below. 
 
[‘Red, ’Green’]
 
For the skip function, the command is given below.
  1. list(islice(Colours ,2, None))   
This method will skip 2 colors from the collection and return the output, as shown below.
 
[‘Blue’, ’Black’, ’White’, ’Orange’, ’Yellow’]
 
TakeWhile & DropWhile
 
Similar to islice, we need to use itertools module. To access it, we need to call takewhile & dropwhile on our page, as shown below.
  1. from itertools import takewhile   
  2. from itertools import dropwhile   
For takewhile function, the command given below is used.
  1. list(takewhile(lambda c: len(c) < 6,Colours))   
This method will take the colors with fewer than 6 letters and return the output, as shown below.
 
[‘Red, ’Green’, ’Blue’, ’Black’, ’White’]
 
For dropwhole function, the command is given below.
 
Here, dropwhile function is similar to skipwhile in C#.
  1. list(dropwhile(lambda c: len(c) < 6,Colours))  
This method will skip the colors with fewer than  6 letters and return an output, as shown below. 
 
[‘Orange’, ’Yellow’]
 
First, FirstOrDefault
 
In Python, the next method is equivalent to FitstOrDefault, but it will not be used on a list
 
For the next method, this method is equivalent to the First method in C#.
  1. next (c for c in Colours if c.startswith(“R”))   
This method will check the colors starting with 'R' and return the first color as “Red”. This method is equivalent to FirstOrDefault method in C#.
  1. next (c for c in Colours if c.startswith(“A”),”none”)   
This method will check the colors starting with 'A' and return the result as “none”, because there is no color starting with the character ‘A’.
 
Count
 
In Python, there is no built-in function like count, as we can use the sum function for the count.
  1. sum(1 for c in Colours if c.startswith(“B”))   
This method will count the colors starting with the letter “B” and the result will be 2.
 
Select & Where
 
In Python map, filter functions will replace the select and where functions in C#. Here are some examples of map and filter functions.
 
For the map method, the command is given below.
  1. list(map(lambda x: x.lower(), Colours))   
This query will return the result, as shown below. 
 
[‘red, ’green’, ’blue’, ’black’, ’white’, ’orange’, ’yellow’]
 
For the filter method, the command is given below.
  1. list(filter(lambda x: "e" in x, Colours))   
This query will return the result, as shown below. 
 
[‘Red, ’Green’, ’Blue’, ’White’, ’Orange’, ’Yellow’]
 
Orderby
 
In Python, the built-in function sorted is used to order the collections,
  1. sorted(Colours, key=lambda x:len(x))   
The sorting is based on the length of colors and the result is shown below. 
 
[‘Red, ’Blue’, ’Green’, ’Black’, ’White’, ’Orange’, ’Yellow’] 

Next Recommended Reading Python Program to Check Odd or Even