0

我有两个班级MoviesListRootObjectResponse. 我想访问 id、Title 和 description 的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; }
}

现在我可以想到编写 LINQ 来获取包含响应的 MovieDetails 对象,但这无济于事。

var movieResponse = from MoviesListRootObject movieDetail in rootObj
    select new MovieDetails
    {
         Response =movieDetail.response
    };
4

3 回答 3

5

目前还不清楚您要达到的目标。我的回答假设您只想拥有所有 MoviesListRootObject 中所有响应的属性:

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

你甚至不需要一个匿名类:

var result = rootObj.SelectMany(x => x.response);
// result will be of type IEnumerable<Response>
于 2012-08-31T11:02:20.567 回答
2
var movieResponse = rootObj
                    .SelectMany(m => m.response)
                    .Select(m => new {
                       id = m.id, // or just m.id, as name is the same
                       title = m.title, //idem
                       description = m.description //idem
                    });
于 2012-08-31T10:58:08.813 回答
1

你问的是SelectMany什么?

var allResponses = rootObj.SelectMany(d => d.Response);

将为您Response提供所有电影的所有内容rootObj


ifrootObj实际上是MoviesListRootObject您不需要的实例SelectMany

var responses = rootObj.response;

会做。

于 2012-08-31T11:05:38.303 回答