8

这是一个例子:

public class FotoLiveLove
{
    public string Tipologia { get; set; }
    public string URL { get; set; }
}

IList<FotoLiveLove> fotoLiveLove = xDoc["statuses"].Select(x => new
{
    Tipologia = "twitter",
    URL = (string)x["URLJSON"]
}).ToList();    

但它说 acnnot 将匿名类型 #1 转换为 FotoLiveLove。

4

3 回答 3

15

您需要在new关键字后添加您的类名:

IList<FotoLiveLove> fotoLiveLove = xDoc["statuses"].Select(x => new FotoLiveLove()
{
    Tipologia = "twitter",
    URL = (string)x["URLJSON"]
}).ToList();  
于 2013-07-16T12:14:41.553 回答
2

您必须在.Select. 尝试类似:

IList<FotoLiveLove> fotoLiveLove = xDoc["statuses"].Select(x => new FotoLiveLove() 
{
    Tipologia = "twitter",
    URL = (string)x["URLJSON"]
}).ToList();  
于 2013-07-16T12:14:48.433 回答
1

在这些情况下,我更喜欢使用查询表单(但这只是一种偏好):

IList<FotoLiveLove> fotoLiveLove = (from f in x.Doc["statuses"]
                               select new FotoLiveLove(){
                                   Tipologia = "twitter",
                                   URL = (string)x["URLJSON"]
                               }).ToList();
于 2013-07-16T12:17:40.647 回答