lora
Uploaded by
7 SLIDES
222 VIEWS
70LIKES

Understanding LINQ: Query and Method Syntax in C#

DESCRIPTION

This guide explores LINQ (Language Integrated Query) in C#, detailing both query and method syntax for performing data operations efficiently. It covers standard query operators such as Where, Select, OrderBy, and the use of lambda expressions for inline operations. The explanation includes examples comparing query syntax with method syntax, while highlighting the benefits of type inference and composability in queries. Learn how to write concise and effective queries in C# to manipulate data easily.

1 / 7

Download Presentation

Understanding LINQ: Query and Method Syntax in C#

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. LINQ Method Syntax

  2. Standard Query Operators • At compile time, query expressions which use the query syntax are translated into method calls. • These methods are called the standard query operators. Some examples are: • Where • Select • OrderBy • Join • Min, Max, Average • You can call them directly by using method syntax instead of query syntax.

  3. static void Main() { int[] numbers = { 5, 10, 8, 3, 6, 12}; //Query syntax: IEnumerable<int> numQuery1 = from num in numbers where num % 2 == 0 orderbynum select num; //Method syntax: IEnumerable<int> numQuery2 = numbers .Where(num => num % 2 == 0) .OrderBy(n => n); foreach (inti in numQuery1) { Console.Write(i + " "); } Console.WriteLine(System.Environment.NewLine); foreach (inti in numQuery2) { Console.Write(i + " "); } } }

  4. Lamda Expressions • The inline highlighted expression is called a lambda expression. • In C# => is the lambda operator, which is read as "goes to". • The num on the left of the operator is the input variable which corresponds to num in the query expression. • The "return value" is just the expression result. IEnumerable<int> numQuery2 = numbers.Where(num => num % 2 == 0).OrderBy(n => n);

  5. Type Inference in Lambdas • Often the type of the input parameters does not have to be specified because the compiler can infer the type. • For most of the standard query operators, the first input is the type of the elements in the source sequence. • So if you are querying an IEnumerable<Customer>, then the input variable is inferred to be a Customer object, which means you have access to its methods and properties: customers.Where(c => c.City == "London");

  6. Composability of Queries IEnumerable<int> numQuery2 = numbers.Where(num => num % 2 == 0).OrderBy(n => n); • The OrderBy method is invoked by using the dot operator on the call to Where. • Where produces a filtered sequence, and then Orderby operates on that sequence by sorting it. • Because queries return an IEnumerable, you compose them in method syntax by chaining the method calls together.

  7. //Query syntax IQuerable<ProductType> etType = from t in entities.ItemTypes where t.ID == ID select new ProductType { ID = t.ID, TypeName = t.TypeName }; //method syntax IQuerable<ProductType> etType = entities.ItemTypes .Where(t => t.ID == ID) .Select(t => new ProductType() { ID = t.ID, TypeName = t.TypeName } );

More Related