博客
关于我
LINQ Tutorial for Beginners
阅读量:798 次
发布时间:2023-01-31

本文共 7479 字,大约阅读时间需要 24 分钟。

LINQ

LINQ is Microsoft’s technology to provide a language-level support mechanism for querying data of all types. These types include in memory arrays and collections, databases, XML documents and more, since version 3.5 and Visual Studio 2008. (.NET 4 and Visual Studio 2010 added support for the Parallel LINQ features).

Need for LINQ

  • Here is the issue that we cannot programmatically interact with a database at the native language level. This means syntax errors often go undetected until runtime.
  • Differing data types utilized by a particular data domain, such as database or XML data types versus the native language
  • XML parsing, iterating, and manipulation can be quite tedious. an XmlDocument must be created just to perform various operations on the XML fragment

Advantages of LINQ

    • Query is integrated into the language. Gone are the days of writing a SQL query into a string and not detecting a syntax error until runtime, for example we forget the name of a field of the table (or reference) or it has been changed in DB.
    • In addition to query features, LINQ to XML provides a more powerful and easier-to-use interface for working with XML data, as though it were a database.

Example:

XElement books = XElement.Parse(       @"
Pro LINQ: Language Integrated Query in C#2010
Joe Rattz
Pro .NET 4.0 Parallel Programming in C#
Adam Freeman
Pro VB 2010 and the .NET 4.0 Platform
Andrew Troelsen
");//XElement Xbooks = XElement.Parse(@"XMLFile.xml");var titles =from book in books.Elements("book")where (string)book.Element("author") == "Joe Rattz"select book.Element("title");foreach (var title in titles) Console.WriteLine(title.Value);
  • Not only for querying data but for formatting, validating, and even getting data into the necessary format : example string array to integer array and sort. can also use select new for complex classes

    Example:

    string[] numbers = { "0042", "010", "9", "27" };            int[] nums = numbers.Select( s => Int32.Parse(s)).OrderBy(s => ).ToArray(); foreach (int num in nums) Console.WriteLine(num);

Syntax of LINQ

Two syntaxes are available for LINQ queries:

    • Query expression syntax
from str in strings where str.Length==3 select str;
    • Standard dot notation syntax
stringList.Where(s => s.Length == 3).Select(s=>s);

Type of LINQ

  • LINQ to Objects
  • LINQ to XML
  • LINQ to DataSet
  • LINQ to SQL
  • LINQ to Entities.

More about LINQ

    • They are queries returning a set of matching objects, a single object, or a subset of fields from an object or set of objects. In LINQ, this returned set of objects is called a sequence, and most LINQ sequences are of typeIEnumerable<T>.
    • We can use the "Cast" or "OfType" operators for Legacy Collections to convert them to IEnumerable to use LINQ.
    • The LINQ query actually take place the first time a result from it is needed. This is typically when the query results variable is enumerated.
    • And this may lead to the situation: deferred query is passed undetected with error.--> deferred query example (disadvantage)

Example:

string[] strings = { "one", "two", null, "three" };Console.WriteLine("Before Where() is called.");IEnumerable
ieStrings = strings.Where(s => s.Length == 3);Console.WriteLine("After Where() is called."); foreach (string s in ieStrings) { Console.WriteLine("Processing " + s); }
    • Calling the actual query each time is needless work. It might make more sense to have a query initialization method that gets called once for the lifetime of the scope and to construct all the queries there (advantage).
List
strings = new List
(); strings.Add("one"); strings.Add("two"); strings.Add("three"); IEnumerable
ieStrings = strings.Where(s => s.Length == 3); foreach (string s in ieStrings) { Console.WriteLine("Processing " + s); } Console.ReadKey(); strings.Add("six"); Console.WriteLine("source enumerable changed but query is not invoked again"); //query is not invoked explicitly, ieStrings is not changes foreach (string s in ieStrings) { Console.WriteLine("Processing " + s); }
    • In Linq To Entity, string can be passed in where clause as a condition using “it” as variable refenrence.

Example:

VCAB_CMS_DBEntities context = new VCAB_CMS_DBEntities(); string condition = "it.DocName=='Shakuntala Devi'"; int pageCount= context.EDOC.Where(condition).Select(c => c.DocPageCount).First();

Language additions with LINQ

Lambda expressions

i => ((i & 1) == 1)

This expression refers to an anonymous method with left side as input to the method and right side as the output.

Expression trees

IEnumerable
numsLessThanFour = nums.Where(i => i < 4).OrderBy(i => i);

The Where operator is called first, followed by the OrderBy operator. But an expression tree allows the simultaneous evaluation and execution of all operators in a query, a single query can be made instead of a separate query for each operator.

The keyword var, object and collection initialization, and anonymous types

var address = new { address = "105 Elm Street",   city = "Atlanta", state = "GA", postalCode = "30339" };

Compiler generated anonymous class name looks like: <>f__AnonymousType5`4[System.String,System.String,System.String,System.String].

Collection initialization allows you to specify the initialization values for a collection, just like you would for an object:

List
presidents = new List
{ "Adams", "Arthur", "Buchanan" };

Extension methods

  • Used to extend a sealed class like adding factorial method in int class or adding todouble method in string class.
  • Extension methods are methods that, although static, can be called on an instance (object) of a class rather than on the class itself.
  • Specifying a method’s first argument with the “this” keyword modifier will make that method an extension method.
  • Extension methods can be declared only in static classes.
public static class ExtendsStringClass{     public static double ToDouble(this string s){         return Double.Parse(s);     } } class Program{ static void Main(string[] args){         double pi = "3.1415926535".ToDouble();         Console.WriteLine(pi);         MyWidget myWidget = new MyWidget();         Console.ReadKey();     }     }

Partial methods

  • A partial method can exist only in a partial class
  • A partial method must return void.
  • Partial methods are private but must not specify the private modifier

Example: To make the generated entity classes more usable, partial methods have been added to them. You can add another module that declares the same entity class, implement these partial methods, and be notified every time a property is about to be changed and after it is changed

Query expressions

  • Query expressions allow LINQ queries to be expressed in nearly SQL form, with just a few minor deviations.
  • The “from” statement precedes the select statement, hence intelliSense has the scope of what variables to offer you for selection.

License

This article, along with any associated source code and files, is licensed under 

转载于:https://www.cnblogs.com/liuzongqi/p/3485108.html

你可能感兴趣的文章
MangoDB4.0版本的安装与配置
查看>>
Manjaro 24.1 “Xahea” 发布!具有 KDE Plasma 6.1.5、GNOME 46 和最新的内核增强功能
查看>>
mapping文件目录生成修改
查看>>
MapReduce程序依赖的jar包
查看>>
mariadb multi-source replication(mariadb多主复制)
查看>>
MariaDB的简单使用
查看>>
MaterialForm对tab页进行隐藏
查看>>
Member var and Static var.
查看>>
memcached高速缓存学习笔记001---memcached介绍和安装以及基本使用
查看>>
memcached高速缓存学习笔记003---利用JAVA程序操作memcached crud操作
查看>>
Memcached:Node.js 高性能缓存解决方案
查看>>
memcache、redis原理对比
查看>>
memset初始化高维数组为-1/0
查看>>
Metasploit CGI网关接口渗透测试实战
查看>>
Metasploit Web服务器渗透测试实战
查看>>
MFC模态对话框和非模态对话框
查看>>
Moment.js常见用法总结
查看>>
MongoDB出现Error parsing command line: unrecognised option ‘--fork‘ 的解决方法
查看>>
mxGraph改变图形大小重置overlay位置
查看>>
MongoDB可视化客户端管理工具之NoSQLbooster4mongo
查看>>