none
What is the usage of let keyword in LINQ RRS feed

  • Question

  • I like to know why and when people use let keyword  in linq?

    Please come with some scenarios which can describe the situation to use let keyword in linq.

    Saturday, November 16, 2019 11:29 AM

Answers

  • Hi Sudip_inn,

    Let keyword is a part of a query expression. It introduces a variable, and then we can reuse the variable elsewhere in the query.

    More details about why and when people use let keyword in linq, you can refer to these documents.

    [C# Let Keyword (Use Variable in Query Expression)]
    [let clause (C# Reference)]

    Best Regards,

    Daniel Zhang

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 18, 2019 7:12 AM

All replies

  • i got few good guide line for Let usage. those links are

    https://www.tutorialsteacher.com/linq/linq-let-keyword
    https://www.c-sharpcorner.com/UploadFile/97fc7a/linq-let-keyword-using-C-Sharp/
    https://www.codeproject.com/Articles/231164/Into-and-Let-in-LINQ-Let-vs-Into

    Saturday, November 16, 2019 7:53 PM
  • Hi Sudip_inn,

    Let keyword is a part of a query expression. It introduces a variable, and then we can reuse the variable elsewhere in the query.

    More details about why and when people use let keyword in linq, you can refer to these documents.

    [C# Let Keyword (Use Variable in Query Expression)]
    [let clause (C# Reference)]

    Best Regards,

    Daniel Zhang

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 18, 2019 7:12 AM
  • Hello,

    As mentioned, the let clause provides the ability to set a variable (sub-expression) within a LINQ statement. In the example below let is used to create a variable lineParts which in this case is a string array which is used to set properties of a class for each line in a text file.

    In this example let is used to create a string array for each row in a DataGridView within a language extension method to write all rows to a file.

    using System;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace StringArrayToIntArray
    {
        public static class DataGridViewExtensions
        {
            public static void ExportRows(this DataGridView sender, string fileName, string defaultNullValue = "(empty)")
            {
                File.WriteAllLines(fileName,
                (
                    from row in sender.Rows.Cast<DataGridViewRow>()
                    where !row.IsNewRow
                    let rowItem = string.Join(",", Array.ConvertAll(((DataGridViewRow)row).Cells.Cast<DataGridViewCell>().ToArray(),
                        c => ((c.Value == null) ? defaultNullValue : c.Value.ToString())))
                    select rowItem
                ));
            }
        }
    
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, November 18, 2019 11:24 AM
    Moderator