What is LINQ in C#.Net?
LINQ stands for "Language Integrated Query". With the help of LINQ, we can directly write query to filter data from a collection using C#.Net. LINQ provides facility to write the query similarly as we do in SQL or any other data base. With the help of LINQ, we can perform filter of data by using where clause, order by to short the data, Different types of joins like Inner Join, Left Outer Join, Right Outer join, count, select rows, group by using C# and many more by using very less no of code.
Class library reference for LINQ is using System.Linq;. Below is an example of LINQ query to select the date from the list.
List<int> data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
var items = from d in data
where d > 7 && d < 12
select d;
foreach (var item in items)
{
Console.WriteLine(item);
}
Console.ReadLine();
In above code I have taken a list of integer type. After that please check the LINQ query to get the value for the list between 7 to 12. Ones we run the code we get output as shown bbelow. How to Count Length of String Using LINQ in C#.Net?
Here I will show you how you can count the number of words available in a string using LINQ with C#.Net. Let's check the below code.
string mainString = "This is an Example of Linq using C#.Net. Here String count we will check. Linq in C# is stands for Language interrated query";
var itemsCount = from s in mainString.Split(' ')
select s.Count();
Console.WriteLine("Total Word Count: "+itemsCount);
In above code I have taken a string in a variable. After that check the next piece of code, where i have user LINQ query to split the string and select the value. You can directly split string also use the code by applying the Count(); Now let's check the final output. Here i will show you how you can count a word occurrence in a string using LINQ in c#.net. Let's check the below code. string mainString = "This is an Example of Linq using C# .net . Here String count we will check. Linq in C# is stands for Language interrated query";
var itemsCount = (from s in mainString.Split(' ')
where s.Equals("Linq")
select s).Count();
Console.WriteLine("Total Word Count: " + itemsCount);
In above code check the LINQ query, where I have first split the string in the where clause I have used ".Equal()" to check for the word which we want to validate how many times it has been occurred. In select the ".Count()" method have been used to count the word occurrence. Now let's run the code to check the output.Here I will show you how you can count the integer value or number appear in a string by using LINQ, c#.net. Let's check the below code. string mainString = "cc6c5c2f-3114-4c47-86f5-96b295efb08f";
Console.WriteLine(mainString);
var itemsCount = (from s in mainString
where Char.IsDigit(s)
select s).Count();
Console.WriteLine("Total Word Count: " + itemsCount);
In above code check the LINQ query in this piece of code I have Char.IsDigit(-), to get all the number value available in a string. After getting all the value I have used "Count()" method to get the count of integer value available in the string. Here I have used a GUID, in this GUID the code will count integer value. Let's run the code and check the output.