簡體   English   中英

LINQ To XML錯誤:select子句中的表達式類型不正確。 調用“選擇”時類型推斷失敗

[英]LINQ To XML error: type of the expression in the select clause is incorrect. Type inference failed in the call to 'Select'

我正在嘗試讀取XML文件,但是type of the expression in the select clause is incorrect. Type inference failed in the call to 'Select'獲取type of the expression in the select clause is incorrect. Type inference failed in the call to 'Select' 由於以下查詢type of the expression in the select clause is incorrect. Type inference failed in the call to 'Select'錯誤type of the expression in the select clause is incorrect. Type inference failed in the call to 'Select'

List<Data> dogs = (from q in doc.Descendants("dog")
    where (string)q.Attribute("name") == dogName
        select new Data
        {
            name = q.Attribute("name").Value,
            breed = q.Element("breed").Value,
            sex = q.Element("sex").Value
        }.ToList<Data>);

資料類別:

public class Data
{
    public string name { get; set; }
    public string breed { get; set; }
    public string sex { get; set; }
    public List<string> dogs { get; set; }
}

問題在於右括號-當您打算將其放在對象初始化程序的末尾時,已將其放在ToList()調用的末尾。 另外,您實際上並沒有調用方法-您只是在指定方法組。 最后,您可以讓類型推斷為您算出type參數:

List<Data> dogs = (from q in doc.Descendants("dog")
                   where (string)q.Attribute("name") == dogName
                   select new Data
                   {
                       name = q.Attribute("name").Value,
                       breed = q.Element("breed").Value,
                       sex = q.Element("sex").Value
                   }).ToList();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM