編輯

共用方式為


Quickstart: Use the Bing Entity Search .NET client library

Use this quickstart to begin searching for entities with the Bing Entity Search client library for C#. While Bing Entity Search has a REST API compatible with most programming languages, the client library provides an easy way to integrate the service into your applications. The source code for this sample can be found on GitHub.

Prerequisites

To add the Bing Entity Search client library to your Visual Studio project, use the Manage NuGet Packages option from Solution Explorer, and add the Microsoft.Azure.CognitiveServices.Search.EntitySearch package.

Create and initialize an application

  1. Create a new C# console solution in Visual Studio. Then add the following into the main code file:

    using System;
    using System.Linq;
    using System.Text;
    using Microsoft.Azure.CognitiveServices.Search.EntitySearch;
    using Microsoft.Azure.CognitiveServices.Search.EntitySearch.Models;
    using Newtonsoft.Json;
    

Create a client and send a search request

  1. Create a new search client. Add your subscription key by creating a new ApiKeyServiceClientCredentials.

    var client = new EntitySearchClient(new ApiKeyServiceClientCredentials("YOUR-ACCESS-KEY"));
    
  2. Use the client's Entities.Search() function to search for your query:

    var entityData = client.Entities.Search(query: "Satya Nadella");
    

Get and print an entity description

  1. If the API returned search results, get the main entity from entityData.

    var mainEntity = entityData.Entities.Value.Where(thing => thing.EntityPresentationInfo.EntityScenario == EntityScenario.DominantEntity).FirstOrDefault();
    
  2. Print the description of the main entity.

    Console.WriteLine(mainEntity.Description);
    

Next steps