官术网_书友最值得收藏!

How to do it...

  1. Open Visual Studio 2017.
  2. Click File | New | Project and, in the New Project template dialog box, select Visual Studio Solutions under the Other Project Types node in the left-hand pane and then Blank Solution in the right-hand pane.
  3. In the Name: text box, type Chapter2.Linq as the name of the solution. Select a preferred location under the Location: drop-down list or click the Browse... button and select a location. Leave the defaults as they are:


  1. Click OK
  2. Now, in the Solution Explorer (or press Ctrl + Alt + L), select Chapter2.Linq. Right-click and select Add | New Project.
  3. In the Add New Project dialog box, expand the Visual C# node and select .NET Standard in the left-hand pane. 
  4. In the right-hand pane, select Class Library (.NET Standard).
  1. Now, in the Name: text box, type Chapter2.Linq.QueriesLib and leave the Location: text box as it is:
  1. Click OK.
  1. Now, the Solution Explorer (press Ctrl + Alt + L to open) should look like this: 
  1. Select Class1.cs in the project tree and press F2
  2. Rename Class1.cs as TelephoneBook.cs, also making sure that you have done the same to the class name itself. 
  3. Answer Yes in the confirmation dialog box for renaming. 
  4. Now, the Solution Explorer should look like this:
  1. Double-click the TelephoneBook.cs file to open the code window.
  2. Now, scroll up to the top of the code window and add the following code next to the last line of the using directives: 
      using System.Collections.Generic;
using System.Linq;
  1. Again, scroll down till you reach the open curly bracket of the TelephoneBook class and add the following code: 
      private List<string> _contactList;
  1.  Now, in the next line, add the following code as the constructor method: 
      public TelephoneBook()
{

_contactList = new List<string>();

_contactList.Add("Lenna Paprocki");
_contactList.Add("Donette Foller");
_contactList.Add("Simona Morasca");
_contactList.Add("Mitsue Tollner");
_contactList.Add("Leota Dilliard");
_contactList.Add("Sage Wieser");
_contactList.Add("Kris Marrier");
_contactList.Add("Minna Amigon");
_contactList.Add("Abel Maclead");
_contactList.Add("Kiley Caldarera");
_contactList.Add("Graciela Ruta");
}
  1. Next to the constructor, add the following code:
      public List<string> GetContacts() 
{
return _contactList;
}
  1. Again, add this code block at the end of the GetContacts() method:
      public List<string> GetContactsByLastName(string lastName)
{
var contacts = _contactList.Where(
c => c.Contains(lastName)).ToList();

return contacts;
}
  1. Finally, add the following code block at the end of the GetContactsByLastName() method: 
      public List<string> GetSortedContacts(bool ascending = true)
{
var sorted = _contactList.OrderBy(c => c).ToList();

if (!ascending)
{
sorted = _contactList.OrderByDescending(c => c).ToList();
}

return sorted;
}
  1. Now that we are done with adding code to the .NET Standard 2.0 library, let's hit Ctrl + Shift + B for a quick build and check for syntax errors. 
主站蜘蛛池模板: 福建省| 海口市| 唐山市| 青岛市| 攀枝花市| 辛集市| 卓资县| 宝丰县| 平果县| 内黄县| 江阴市| 洞头县| 汉沽区| 万盛区| 电白县| 开原市| 津南区| 巍山| 鄂温| 大荔县| 边坝县| 涞源县| 咸丰县| 朝阳区| 山阴县| 松潘县| 理塘县| 无棣县| 湖北省| 正阳县| 邳州市| 昆山市| 阿拉善盟| 太仆寺旗| 怀来县| 喀什市| 七台河市| 朝阳县| 桐梓县| 灵宝市| 海城市|