我想查詢一個DocumentDB集合,並通過一個平穩的ASP.Net Web API控制器返回結果。我有姓名和出生日期,以及AthletesController一個簡單的運動員類,它包含以下方法:如何以列表形式返回DocumentDB查詢<Type>?
[HttpGet]
public List<Athlete> listAthletes()
{
string endpoint = ConfigurationManager.AppSettings["endpoint"];
string authkey = ConfigurationManager.AppSettings["authkey"];
string database = ConfigurationManager.AppSettings["database"];
string collection = "Athletes";
DocumentClient client = new DocumentClient(new Uri(endpoint), authkey);
List<Athlete> response = client.CreateDocumentQuery("dbs/" + database + "/colls/" + collection, "SELECT * FROM Athletes").AsEnumerable().Cast<Athlete>().ToList();
return response;
}
的總體思路是,我轉換的IQueryable成一個IEnumerable,將它轉換爲Type運動員,然後使用ToList方法使其可以用於Web API消費。
然而,這是我在運行時出現錯誤:
無法轉換類型 'Microsoft.Azure.Documents.QueryResult' 的對象鍵入 'TestApp.Models.Athlete'
你剛剛做了ToList() – qamar
是的,那不會編譯。 錯誤\t CS0029 \t無法隱式轉換類型「System.Collections.Generic.List」到「System.Collections.Generic.List 」 –
Graham
嗯,我想下一個合乎邏輯的步驟將是選擇做一個LINQ既然你已經把結果轉化爲可挖。只要嘗試選擇並建立運動員的集合。直接投射可能不可能 – qamar