Tuesday, April 20, 2021

LINQ let Clause Examples

LINQ has three parts:
  1. Data source 
  2. Query of data source
  3. Execution of Query
A query expression must begin with a from clause. Additionally, a query expression can contain sub-queries, which also begin with a from clause.

The from clause specifies the following:
  • The data source on which the query or sub-query will be run.
  • A local range variable that represents each element in the source sequence.
  • Both the range variable and the data source are strongly typed.

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/from-clause
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/let-clause


EXAMPLE 1


using System.Linq;
using System.Web.Mvc;

namespace Mvc_LINQ_let_clause.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public string Index()
        {
            //data source
            string[] Quotes =
            {
            "Everything you can imagine is real.",
            "Whatever you do, do it well.",
            "What we think, we become."
            };

            //LINQ query
            var Query =
            from sentence in Quotes
            let words = sentence.Split(' ')
            from word in words
            select word;

            
            //Excecute Query
            foreach (var letter in Query)
            {
                Response.Write(letter);
                Response.Write("
"); } return ""; } } }

OUTPUT:
Everything
you
can
imagine
is
real.
Whatever
you
do,
do
it
well.
What
we
think,
we
become.

EXAMPLE 2


using System.Linq;
using System.Web.Mvc;

namespace Mvc_LINQ_let_clause.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public string Index()
        {
            //data source
            string[] Quotes =
            {
            "Everything you can imagine is real.",
            "Whatever you do, do it well.",
            "What we think, we become."
            };

            //LINQ query
            var Query =
            from quote in Quotes
            where quote.Contains("What")
            select quote.ToUpper();

            
            //Excecute Query
            foreach (var WhatQuote in Query)
            {
                Response.Write(WhatQuote);
                Response.Write("
"); } return ""; } } }

OUTPUT:

WHATEVER YOU DO, DO IT WELL.
WHAT WE THINK, WE BECOME.

EXAMPLE 3


using System.Linq;
using System.Web.Mvc;

namespace Mvc_LINQ_let_clause.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public string Index()
        {
            //data source
            string[] Quotes =
            {
            "Everything you can imagine is real.",
            "Whatever you do, do it well.",
            "What we think, we become."
            };

            //LINQ query
            var Query =
            from quote in Quotes
            where quote.Contains("What")
            //Note Split function has charater parameter in single quotation mark
            let clauses = quote.Split(',')
            from clause in clauses
            select clause.ToUpper();

            
            //Excecute Query
            foreach (var cl in Query)
            {
                Response.Write(cl);
                Response.Write("
"); } return ""; } } }

OUTPUT:

WHATEVER YOU DO
DO IT WELL.
WHAT WE THINK
WE BECOME.

No comments:

Post a Comment

Hot Topics