Learn LINQ to SQL (i): Preliminary knowledge

Source: Internet
Author: User
Tags foreach anonymous new features static class

What is LINQ to SQL

LINQ to SQL (or DLinq) is part of LINQ (. NET language integration queries), A. NET language integrated query based on relational data, used to manage relational data in an object form, and provides rich query capabilities, and LINQ to XML, LINQ to Objects, LINQ to DataSet, LINQ to Entities, etc. compose powerful LINQ.

To learn the LINQ query syntax, you have to first understand some of the new features of C # 3.0, as described in the following one by one brief.

Implicitly type local variable

var age = 26;
var username = "zhuye";
var userlist = new [] {"a","b","c"};
foreach(var user in userlist)
Console.WriteLine(user);

Just for lazy people using the var keyword, tell the compiler (for the CLR, it will not know if you are using Var, coolie is the compiler out), you infer its type, I do not care. But since the compiler infers the type must be declared when the value is assigned, and cannot be a null value. Note that this can only be used for local variables, which are not available for fields.

Anonymous type

var data = new {username = "zhuye",age = 26};
Console.WriteLine("username:{0} age:{1}", data.username, data.age);

An anonymous type allows a developer to define a row type without explicitly defining the type. Used in conjunction with VAR, VAR is used to declare anonymous types. Defining a temporary anonymous type is very common in LINQ query syntax, and we can easily implement object conversion and projection.

Extension methods

public static class helper
  {
    public static string MD5Hash(this string s)
    {
      return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(s,"MD5");
    }
    public static bool In(this object o, IEnumerable b)
    {
      foreach(object obj in b)
      {
        if(obj==o)
        return true;
      }
      return false;
    }    
}

// 调用扩展方法
Console.WriteLine("123456".MD5Hash());
Console.WriteLine("1".In(new[]{"1","2","3"}));

A lot of times we need to do something with CLR types, we can't extend the methods of CLR types, we can only create some helper methods, or generate subclasses. Extension methods enable these requirements to be realized, but also the basis for the implementation of LINQ. Defining an extension method requires attention, which can only be defined in a static class and is a static method, and the extension method will fail if the extension method name conflicts with the original method name.

Automatic properties

public class Person
  {
    public string username { get; protected set; }
    public int age { get; set; }
    public Person()
    {
      this.username = "zhuye";      
    }
}

Person p = new Person();
//p.username = "aa";
Console.WriteLine(p.username);

Meaning is not very large, purely to solve mechanical labor. The compiler automatically generates get, set operations, and fields for you, and you cannot use a field and you cannot customize get and set operations, but you can define the access level for both a and set.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.