我可以直接在LINQ中填充我自己的类列表吗?

mar*_*zzz 8 c# linq

这是一个例子:

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();    
Run Code Online (Sandbox Code Playgroud)

但它表示无法将匿名类型#1转换为FotoLiveLove.

Cod*_*gue 15

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

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

  • +1.另外,如果使用对象初始化程序语法,则不需要`()`. (5认同)