使用LINQ如何访问类中的List项的值

Sim*_*ons 0 .net c# linq

我有两个类MoviesListRootObjectResponse.我想要访问的id,标题和描述的MoviesListRootObjectconatins列表并将其分配给var.ResponseList<Response>

public class MoviesListRootObject
{
    public int count { get; set; }
    public Pagination pagination { get; set; }
    public List<Response> response { get; set; }
}
[Serializable]
public class Response
{
    public int id { get; set; }
    public string title { get; set; }
    public string title_language { get; set; }
    public string description { get; set; }
    public string description_language { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

现在,我能想到编写LINQ来获取包含响应的MovieDetails对象,但这不会有帮助.

var movieResponse = from MoviesListRootObject movieDetail in rootObj
    select new MovieDetails
    {
         Response =movieDetail.response
    };
Run Code Online (Sandbox Code Playgroud)

Dan*_*rth 5

你想要实现的目标并不是很清楚.我的回答假设您只想拥有所有MoviesListRootObject中所有响应的属性:

var result = rootObj.SelectMany(x => x.response)
                    .Select(x => new { x.id, x.title, x.description });
Run Code Online (Sandbox Code Playgroud)

你甚至不需要匿名课程:

var result = rootObj.SelectMany(x => x.response);
// result will be of type IEnumerable<Response>
Run Code Online (Sandbox Code Playgroud)