Phím tắt trong Visual Studio.Net

Làm việc với một môi trường lập trình chuyên nghiệp như .Net bạn cũng cần phải chuyên nghiệp hóa bản thân. Phím tắt là một công cụ rất hữu dụng làm tăng tốc độ coding của bạn. Bài viết này tôi xin giới thiệu với các bạn một số tổ hợp phím tắt hay được dùng trong Visual Studio.Net 

Dưới đây là bảng chi tiết về các phím tắt dùng để thao tác với văn bản khi bạn làm việc với Visual Studio. Net. Hi vọng nó có ích cho công việc của bạn. Chúc bạn học tốt.
Phần 1





Phần 2:
giới thiệu một số phím dùng khi bạn tìm kiếm văn bản



Phần 3


phần 4
Bài này giới thiệu với các bạn phím tắt khi làm việc với trình gỡ lỗi



Phần 5:






phần 6:

Làm thế nào để thực hiện Inner Join (C#)

Trong điều kiện cơ sở dữ liệu quan hệ, một bên tham gia sản xuất một tập kết quả, trong đó mỗi phần tử của các bộ sưu tập đầu tiên xuất hiện một lần cho tất cả các yếu tố phù hợp trong bộ sưu tập thứ hai. Nếu một thành phần trong bộ sưu tập đầu tiên không có các yếu tố phù hợp, nó không xuất hiện trong tập kết quả. Các phương pháp tham gia, được gọi là do tham gia điều khoản trong C#, thực hiện một nội tâm tham gia.

Chủ đề này cho bạn thấy làm thế nào để thực hiện bốn biến thể của một bên tham gia:

Một đơn giản bên tham gia có tương quan các yếu tố từ hai nguồn dữ liệu dựa trên một khóa đơn giản.

Một trong những yếu tố tham gia có tương quan từ hai nguồn dữ liệu dựa trên một khóa composite. Một chính tổng hợp, mà là một chính mà bao gồm nhiều hơn một giá trị, cho phép bạn tương quan các yếu tố dựa trên nhiều hơn một tài sản.

Một đa tham gia, trong đó tiếp tham gia các hoạt động được nối với nhau.

Một trong tham gia được thực hiện bằng cách sử dụng một nhóm tham gia.

Ví dụ về Simple Key Join :

Ví dụ sau tạo ra hai bộ sưu tập có chứa các đối tượng của hai kiểu người dùng định nghĩa, người và vật nuôi. Truy vấn này sử dụng mệnh đề join trong C# để phù hợp với nhân vật với các đối tượng mà chủ Pet là người. Các mệnh đề select trong C# định nghĩa cách sẽ xem xét các kết quả đối tượng. Trong ví dụ này, các kết quả đối tượng là loại vô danh đó bao gồm tên của chủ sở hữu và tên của vật nuôi.

class Person
{
       public string FirstName { get; set; }
       public string LastName { get; set; }
}
class Pet
{
       public string Name { get; set; }
       public Person Owner { get; set; }
}
///
/// Simple inner join.
///
public static void InnerJoinExample()
{
       Person magnus = new Person { FirstName = "Magnus", LastName = "Hedlund" };
       Person terry = new Person { FirstName = "Terry", LastName = "Adams" };
       Person charlotte = new Person { FirstName = "Charlotte", LastName = "Weiss" };
       Person arlene = new Person { FirstName = "Arlene", LastName = "Huff" };
       Person rui = new Person { FirstName = "Rui", LastName = "Raposo" };
       Pet barley = new Pet { Name = "Barley", Owner = terry };
       Pet boots = new Pet { Name = "Boots", Owner = terry };
       Pet whiskers = new Pet { Name = "Whiskers", Owner = charlotte };
       Pet bluemoon = new Pet { Name = "Blue Moon", Owner = rui };
       Pet daisy = new Pet { Name = "Daisy", Owner = magnus };
       // Create two lists.
       List<Person> people = new List<Person> { magnus, terry, charlotte, arlene, rui };
       List<Pet> pets = new List<Pet> { barley, boots, whiskers, bluemoon, daisy };
       // Create a collection of person-pet pairs. Each element in the collection
       // is an anonymous type containing both the person's name and their pet's name.
       var query = from person in people join pet in pets on person equals pet.Owner
            select new { OwnerName = person.FirstName, PetName = pet.Name };
       foreach (var ownerAndPet in query)
       {
             Console.WriteLine("\"{0}\" is owned by {1}", ownerAndPet.PetName, 
                     ownerAndPet.OwnerName);
       }
}
// This code produces the following output:
// "Daisy" is owned by Magnus
// "Barley" is owned by Terry
// "Boots" is owned by Terry
// "Whiskers" is owned by Charlotte
// "Blue Moon" is owned by Rui
Ví dụ về Composite Key Join :
Ví dụ sau đây sử dụng một danh sách các đối tượng lao động và danh sách các đối tượng sinh viên để xác định các nhân viên cũng được học sinh. Cả hai loại có FirstName và sở hữu một LastName kiểu String. Các chức năng đó tạo ra các khoá kết nối từ các yếu tố của mỗi danh sách trả về một kiểu nặc danh mà bao gồm các tài sản FirstName và LastName của mỗi phần tử. Các hoạt động tham gia so sánh các tổ hợp phím bình đẳng và trả về cặp của các đối tượng từ danh sách mỗi nơi cả hai cái tên đầu tiên và tên những trận đấu cuối cùng.
class Employee
{
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public int EmployeeID { get; set; }
}
class Student
{
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public int StudentID { get; set; }
}
///
/// Performs a join operation using a composite key.
///
public static void CompositeKeyJoinExample()
{
      // Create a list of employees.
      List<Employee> employees = new List<Employee> {
            new Employee { FirstName = "Terry", LastName = "Adams", EmployeeID = 522459 },
            new Employee { FirstName = "Charlotte", LastName = "Weiss", EmployeeID = 204467 },
            new Employee { FirstName = "Magnus", LastName = "Hedland", EmployeeID = 866200 },
            new Employee { FirstName = "Vernette", LastName = "Price", EmployeeID = 437139 } 
      };
      // Create a list of students.
      List<Student> students = new List<Student> {
            new Student { FirstName = "Vernette", LastName = "Price", StudentID = 9562 },
            new Student { FirstName = "Terry", LastName = "Earls", StudentID = 9870 },
            new Student { FirstName = "Terry", LastName = "Adams", StudentID = 9913 } 
      };
      // Join the two data sources based on a composite key consisting of first and last name,
      // to determine which employees are also students.
      IEnumerable<string> query = from employee in employees join student in students
               on new { employee.FirstName, employee.LastName }
               equals new { student.FirstName, student.LastName }
               select employee.FirstName + " " + employee.LastName;
      Console.WriteLine("The following people are both employees and students:");
      foreach (string name in query)
              Console.WriteLine(name);
      }
// This code produces the following output:
//
// The following people are both employees and students:
// Terry Adams
// Vernette Price
Ví dụ về Multiple Join :
class Person
{
      public string FirstName { get; set; }
      public string LastName { get; set; }
}
class Pet
{
      public string Name { get; set; }
      public Person Owner { get; set; }
}
class Cat : Pet { }
class Dog : Pet { }
public static void MultipleJoinExample()
{
      Person magnus = new Person { FirstName = "Magnus", LastName = "Hedlund" };
      Person terry = new Person { FirstName = "Terry", LastName = "Adams" };
      Person charlotte = new Person { FirstName = "Charlotte", LastName = "Weiss" };
      Person arlene = new Person { FirstName = "Arlene", LastName = "Huff" };
      Person rui = new Person { FirstName = "Rui", LastName = "Raposo" };
      Person phyllis = new Person { FirstName = "Phyllis", LastName = "Harris" };
      Cat barley = new Cat { Name = "Barley", Owner = terry };
      Cat boots = new Cat { Name = "Boots", Owner = terry };
      Cat whiskers = new Cat { Name = "Whiskers", Owner = charlotte };
      Cat bluemoon = new Cat { Name = "Blue Moon", Owner = rui };
      Cat daisy = new Cat { Name = "Daisy", Owner = magnus };
      Dog fourwheeldrive = new Dog { Name = "Four Wheel Drive", Owner = phyllis };
      Dog duke = new Dog { Name = "Duke", Owner = magnus };
      Dog denim = new Dog { Name = "Denim", Owner = terry };
      Dog wiley = new Dog { Name = "Wiley", Owner = charlotte };
      Dog snoopy = new Dog { Name = "Snoopy", Owner = rui };
      Dog snickers = new Dog { Name = "Snickers", Owner = arlene };
      // Create three lists.
      List<Person> people = new List<Person> { magnus, terry, charlotte, arlene, rui, phyllis };
      List<Cat> cats = new List<Cat> { barley, boots, whiskers, bluemoon, daisy };
      List<Dog> dogs = new List<Dog> { fourwheeldrive, duke, denim, wiley, snoopy, snickers };
      // The first join matches Person and Cat.Owner from the list of people and
     // cats, based on a common Person. The second join matches dogs whose names start
     // with the same letter as the cats that have the same owner. 
     var query = from person in people join cat in cats on person equals cat.Owner
           join dog in dogs on new { Owner = person, Letter = cat.Name.Substring(0, 1) }
          equals new { dog.Owner, Letter = dog.Name.Substring(0, 1) }
          select new { CatName = cat.Name, DogName = dog.Name };
      foreach (var obj in query)
      {
           Console.WriteLine( "The cat \"{0}\" shares a house, and the first letter of their name, with  \"{1}\".", obj.CatName, obj.DogName);
      }
}
// This code produces the following output:
//
// The cat "Daisy" shares a house, and the first letter of their name, with "Duke".
// The cat "Whiskers" shares a house, and the first letter of their name, with "Wiley".
Inner Join bằng cách sử dụng Grouped Join
class Person
{
        public string FirstName { get; set; }
        public string LastName { get; set; }
}


class Pet
{
        public string Name { get; set; }
        public Person Owner { get; set; }
}


/// <summary>
/// Performs an inner join by using GroupJoin().
/// </summary>
public static void InnerGroupJoinExample()
{
       Person magnus = new Person { FirstName = "Magnus", LastName = "Hedlund" };
       Person terry = new Person { FirstName = "Terry", LastName = "Adams" };
       Person charlotte = new Person { FirstName = "Charlotte", LastName = "Weiss" };
       Person arlene = new Person { FirstName = "Arlene", LastName = "Huff" };


       Pet barley = new Pet { Name = "Barley", Owner = terry };
       Pet boots = new Pet { Name = "Boots", Owner = terry };
       Pet whiskers = new Pet { Name = "Whiskers", Owner = charlotte };
       Pet bluemoon = new Pet { Name = "Blue Moon", Owner = terry };
       Pet daisy = new Pet { Name = "Daisy", Owner = magnus };


       // Create two lists.
       List<Person> people = new List<Person> { magnus, terry, charlotte, arlene };
       List<Pet> pets = new List<Pet> { barley, boots, whiskers, bluemoon, daisy };


       var query1 = from person in people join pet in pets on person equals pet.Owner into gj
             from subpet in gj select new { OwnerName = person.FirstName, PetName = subpet.Name };


       Console.WriteLine("Inner join using GroupJoin():");
       foreach (var v in query1)
       {
                Console.WriteLine("{0} - {1}", v.OwnerName, v.PetName);
       }


      var query2 = from person in people  join pet in pets on person equals pet.Owner
              select new { OwnerName = person.FirstName, PetName = pet.Name };


      Console.WriteLine("\nThe equivalent operation using Join():");
      foreach (var v in query2) 
      {
            Console.WriteLine("{0} - {1}", v.OwnerName, v.PetName);
     }


// This code produces the following output:
//
// Inner join using GroupJoin():
// Magnus - Daisy
// Terry - Barley
// Terry - Boots
// Terry - Blue Moon
// Charlotte - Whiskers
//
// The equivalent operation using Join():
// Magnus - Daisy
// Terry - Barley
// Terry - Boots
// Terry - Blue Moon
// Charlotte - Whiskers

Từ khoá truy vấn trong LINQ (Query Keywords)

Bảng liệt kê các từ khóa sử dụng trong biểu thức truy vấn trong.

FROM : Chỉ định một nguồn dữ liệu và biến một phạm vi (tương tự như một biến lặp đi lặp lại).

WHERE : Bộ lọc các yếu tố nguồn dựa trên một hoặc nhiều biểu thức Boolean cách nhau bởi logic AND và OR điều hành (&& hoặc | |).

SELECT : Chỉ định kiểu và hình dạng mà các phần tử trong chuỗi trở về sẽ có khi truy vấn được thực hiện.

GROUP : Nhóm kết quả truy vấn theo một giá trị chính quy định.

INTO : Cung cấp một định danh có thể phục vụ như là một tham chiếu đến các kết quả của một nhóm tham gia, hoặc điều khoản lựa chọn.

ORDERBY : Phân loại các kết quả truy vấn trong tăng hay giảm dựa trên Comparer mặc định cho các loại nguyên tố.

JOIN : tham gia của hai nguồn dữ liệu dựa trên sự so sánh bình đẳng giữa hai tiêu chuẩn quy định phù hợp.

LET : giới thiệu một loạt biến để lưu trữ phụ biểu kết quả trong một biểu thức truy vấn.

IN : theo ngữ cảnh từ khoá trong một gia khoản.

ON : theo ngữ cảnh từ khoá trong một gia khoản.

EQUALS : theo ngữ cảnh từ khoá trong một gia khoản.

BY: theo ngữ cảnh từ khoá trong một điều khoản nhóm.

ASCENDING : từ khóa theo ngữ cảnh trong một khoản orderby.

DESXENDING : từ khóa theo ngữ cảnh trong một khoản orderby.

Example : 

FROM : 

class LowNums
{
        static void Main()
        { 
               // A simple data source.
               int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
               // Create the query.
               // lowNums is an IEnumerable<int>
               var lowNums = from num in numbers where num < 5 select num;


              // Execute the query.
              foreach (int i in lowNums)
              {
                     Console.Write(i + " ");
              }
       } 
}
// Output: 4 1 3 2 0

WHERE :

class WhereSample2
{
        static void Main()
       { 
               // Data source.
               int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
               // Create the query with two predicates in where clause.
               var queryLowNums2 =  from num in numbers where num < 5 && num % 2 == 0
                     select num;
               // Execute the query
               foreach (var s in queryLowNums2)
              {
                    Console.Write(s.ToString() + " ");
              } 
      }
}
// Output: 4 2 0

GROUP :

// Query variable is an IEnumerable<IGrouping<char, Student>>
var studentQuery1 = from student in students group student by student.Last[0];

INTO :

class IntoSample1
{
         static void Main()
         {
                // Create a data source.
                string[] words = {"apples","blueberries","oranges","bananas","apricots"};
               // Create the query.
               var wordGroups1 = from w in words group w by w[0] into fruitGroup
                      where fruitGroup.Count() >= 2
                      select new { FirstLetter = fruitGroup.Key, Words = fruitGroup.Count() };
              // Execute the query. Note that we only iterate over the groups, 
              // not the items in each group
              foreach (var item in wordGroups1)
              {
                     Console.WriteLine(" {0} has {1} elements.", item.FirstLetter, item.Words);
              }

              // Keep the console window open in debug mode
              Console.WriteLine("Press any key to exit.");
              Console.ReadKey();
       }
}
/* Output:
a has 2 elements.
b has 2 elements.
*/

Làm thế nào để thực hiện Grouped Join

Các group join rất hữu ích cho tạo ra các cấu trúc phân cấp dữ liệu. Những cặp này mỗi yếu tố từ các bộ sưu tập đầu tiên với một tập hợp các yếu tố tương quan từ các bộ sưu tập thứ hai.

Ví dụ, một class hoặc cơ sở dữ liệu một bảng quan hệ có tên là Sinh viên có thể chứa hai lĩnh vực: Id và Name. Một class thứ hai hoặc bảng cơ sở dữ liệu quan hệ có tên là khóa học có thể có hai lĩnh vực: StudentId và CourseTitle. Một nhóm tham gia của hai nguồn dữ liệu, dựa trên kết hợp Student.Id và Course.StudentId, sẽ từng nhóm sinh viên với một bộ sưu tập của các đối tượng học.

Ví dụ đầu tiên trong chủ đề này cho bạn thấy làm thế nào để thực hiện một group join. Ví dụ thứ hai chỉ cho bạn cách sử dụng một nhóm join để tạo ra các phần tử XML.

Ví dụ Group Join :

Ví dụ sau đây thực hiện một nhóm join của các đối tượng của loại hình Person và PET dựa trên những người phù hợp với property Pet.Owner. Không giống như một group không join, mà sẽ tạo ra một cặp của các yếu tố cho mỗi trận đấu, các group join tạo ra chỉ có một kết quả là đối tượng cho từng thành phần của bộ sưu tập đầu tiên, mà trong ví dụ này là một đối tượng Person. Các yếu tố tương ứng từ các bộ sưu tập thứ hai, trong ví dụ này là Pet đối tượng, được nhóm lại thành một bộ sưu tập. Cuối cùng, chức năng kết quả chọn tạo ra một loại giấu tên cho mỗi trận đấu mà bao gồm Person.FirstName và một bộ sưu tập của các đối tượng Pet.

class Person
{
        public string FirstName { get; set; }
        public string LastName { get; set; }
}
class Pet
{
        public string Name { get; set; }
        public Person Owner { get; set; }
}
/// <summary>
/// This example performs a grouped join.
/// </summary>
public static void GroupJoinExample()
{
        Person magnus = new Person { FirstName = "Magnus", LastName = "Hedlund" };
        Person terry = new Person { FirstName = "Terry", LastName = "Adams" };
        Person charlotte = new Person { FirstName = "Charlotte", LastName = "Weiss" };
        Person arlene = new Person { FirstName = "Arlene", LastName = "Huff" };
        Pet barley = new Pet { Name = "Barley", Owner = terry };
        Pet boots = new Pet { Name = "Boots", Owner = terry };
        Pet whiskers = new Pet { Name = "Whiskers", Owner = charlotte };
        Pet bluemoon = new Pet { Name = "Blue Moon", Owner = terry };
        Pet daisy = new Pet { Name = "Daisy", Owner = magnus };
        // Create two lists.
        List<Person> people = new List<Person> { magnus, terry, charlotte, arlene };
        List<Pet> pets = new List<Pet> { barley, boots, whiskers, bluemoon, daisy };
        // Create a list where each element is an anonymous type
        // that contains the person's first name and a collection of
        // pets that are owned by them.
        var query = from person in people
                 join pet in pets on person equals pet.Owner into gj
                 select new { OwnerName = person.FirstName, Pets = gj };
        foreach (var v in query)
        {
               // Output the owner's name.
              Console.WriteLine("{0}:", v.OwnerName);
              // Output each of the owner's pet's names.
              foreach (Pet pet in v.Pets)
                    Console.WriteLine(" {0}", pet.Name);
        }
}

Group join rất lý tưởng cho việc tạo ra XML bằng cách sử dụng LINQ to XML. Ví dụ sau là tương tự như ví dụ trước, ngoại trừ thay vì tạo ra các loại giấu tên, chức năng chọn kết quả tạo ra các phần tử XML mà đại diện cho các đối tượng tham gia.

Bạn chỉ cần thay đổi dòng dưới cho ví dụ ở trên.

// Create XML to display the hierarchical organization of people and their pets.
XElement ownersAndPets = new XElement("PetOwners", 
       from person in people join pet in pets on person equals pet.Owner into gj
                select new XElement("Person",
                        new XAttribute("FirstName", person.FirstName),
                        new XAttribute("LastName", person.LastName),
                        from subpet in gj
                            select new XElement("Pet", subpet.Name)));
Console.WriteLine(ownersAndPets);

Làm thế nào để sử dụng Left Outer Joins

Một left outer join là một join, trong đó mỗi phần tử của các bộ sưu tập đầu tiên được t

class Person
{
       public string FirstName { get; set; }
       public string LastName { get; set; }
}

class Pet
{
       public string Name { get; set; }
       public Person Owner { get; set; }
}


public static void LeftOuterJoinExample()
{
       Person magnus = new Person { FirstName = "Magnus", LastName = "Hedlund" };
       Person terry = new Person { FirstName = "Terry", LastName = "Adams" };
       Person charlotte = new Person { FirstName = "Charlotte", LastName = "Weiss" };
       Person arlene = new Person { FirstName = "Arlene", LastName = "Huff" };
       Pet barley = new Pet { Name = "Barley", Owner = terry };
       Pet boots = new Pet { Name = "Boots", Owner = terry };
       Pet whiskers = new Pet { Name = "Whiskers", Owner = charlotte };
       Pet bluemoon = new Pet { Name = "Blue Moon", Owner = terry };
       Pet daisy = new Pet { Name = "Daisy", Owner = magnus };
       // Create two lists.
       List<Person> people = new List<Person> { magnus, terry, charlotte, arlene };
       List<Pet> pets = new List<Pet> { barley, boots, whiskers, bluemoon, daisy };
       var query = from person in people
              join pet in pets on person equals pet.Owner into gj
              from subpet in gj.DefaultIfEmpty()
              select new { 
                      person.FirstName, PetName = (subpet == null ? String.Empty : subpet.Name)   
              };
       foreach (var v in query)
       {
              Console.WriteLine("{0,-15}{1}", v.FirstName + ":", v.PetName);
       }
}
// This code produces the following output:
//
// Magnus: --- > Daisy
// Terry: --- > Barley
// Terry: --- >  Boots
// Terry: --- > Blue Moon
// Charlotte: --- > Whiskers
// Arlene: --- >
rả về, bất kể nó có bất kỳ yếu tố tương quan trong bộ sưu tập thứ hai. Bạn có thể sử dụng LINQ để thực hiện một left outer join bằng cách gọi DefaultIfEmpty kết quả của một group join.

Ví dụ sau đây cho thấy làm thế nào để sử dụng Methos DefaultIfEmpty kết quả của một group join để thực hiện một left outer join.

Bước đầu tiên trong việc tạo ra một left outer join trong hai bộ sưu tập là để thực hiện một bên join bằng cách sử dụng một group join. Trong ví dụ này, danh sách các đối tượng Person inner join vào danh sách các đối tượng PET dựa trên một Person object phù hợp với Pet.Owner.

Bước thứ hai là để bao gồm mỗi yếu tố của bộ sưu tập (left) đầu tiên trong tập hợp kết quả ngay cả khi không có yếu tố phù hợp trong bộ sưu tập phải. Điều này được thực hiện bằng cách gọi DefaultIfEmpty trên mỗi chuỗi kết hợp các yếu tố từ các group join. Trong ví dụ này, DefaultIfEmpty được gọi vào mỗi chuỗi các đối tượng phù hợp với PET. Nó trả về một bộ sưu tập có chứa một giá trị, mặc định duy nhất nếu các trình tự của các đối tượng phù hợp PET trống cho bất kỳ đối tượng Person, qua đó đảm bảo rằng kết quả mỗi đối tượng Person được đại diện trong bộ sưu tập.