如何在lambda表达式中定义LINQ Select Method中的另一个函数

时间:2016-02-10 23:37:45

标签: c# linq azure lambda

我正在创建Azure移动应用。 在api中,我使用Entity Framework 6.1创建了以下列表。 问题是它在Select方法中太大了,

public IHttpActionResult GetAllQABundle(string qAUser_id, int offset = 0, int limit = 20)
    {

        List<QABundleWithCommentDto> qABundleWithCommentDtos = context.QABundles
            .Where(b => b.TeamId == null)
            .OrderByDescending(b => b.UpdatedAt)
            .Skip(offset)
            .Take(limit)
            .Select(b => new QABundleWithCommentDto()
            {
                Id = b.Id,
                TagForFreeFormat = b.TagForFreeFormat,
                MovieUrl = b.Movie.MovieUrl,
                MovieThumbnailUrl = b.Movie.MovieThumbnailUrl,
                MovieStreamUrl = b.Movie.MovieStreamUrl,
                NumberOfViews = b.Movie.NumberOfViews,
                Title = b.Title,
                Description = b.Description,
                QuestionType = b.QuestionType,
                WannaKnowId = b.WannaKnows.Where(w => w.QAUserId == qAUser_id).FirstOrDefault().Id,
                WannaKnowCount = b.WannaKnows.Count,
                QAUserThumbnailUrl = b.QAUser.ThumbnailUrl,
                QAUserId = b.QAUser.Id,
                UserName = b.QAUser.UserName,
                UpdatedAt = b.UpdatedAt,
                GenereDtos = b.Generes.Select(t => new GenereDto() { GenereId = t.Id, GenereName = t.Name }).ToList(),
                CommentDtos = b.Comments.OrderByDescending(c => c.UpdatedAt).Take(20).Select(c => new CommentDto()
                {
                    Id = c.Id,
                    Text = c.Text,
                    MovieUrl = c.Movie.MovieUrl,
                    MovieThumbnailUrl = c.Movie.MovieThumbnailUrl,
                    QAUserId = c.QAUserId,
                    UserName = c.QAUser.UserName,
                    QAUserBelongsTo = c.QAUser.BelongsTo,
                    QAUserThumbnailUrl = c.QAUser.ThumbnailUrl,
                    CommentCreatedAt = c.CreatedAt,
                    Likes = c.Likes.Count,
                    LikeId = c.Likes.Where(x => x.QAUserId == qAUser_id).FirstOrDefault().Id,
                    UpdatedAt = c.UpdatedAt,
                    QABundleId = c.QABundleId
                }).ToList(),
                TeamDto = new TeamDto()
                {
                    Id = b.TeamId,
                    Name = b.Team.Name,
                    FollowedDtos = b.Team.QAUsers.Select(u => new FollowedDto()
                    {
                        Id = u.Id,
                        UserName = u.UserName,
                        ThumbnailUrl = u.ThumbnailUrl,
                    }).ToList()
                },
                BestAnswerDto = new BestAnswerDto() { Id = b.BestAnswerId, CommentId = b.BestAnswer.CommentId }
            })
            .ToList();

        return Json(qABundleWithCommentDtos);
    }

我想将以下部分定义为另一个函数,但我不知道如何实现它。 有没有人有好主意?

b => new QABundleWithCommentDto()
            {
                Id = b.Id,
                TagForFreeFormat = b.TagForFreeFormat,
                MovieUrl = b.Movie.MovieUrl,
                MovieThumbnailUrl = b.Movie.MovieThumbnailUrl,
                MovieStreamUrl = b.Movie.MovieStreamUrl,
                NumberOfViews = b.Movie.NumberOfViews,
                Title = b.Title,
                Description = b.Description,
                QuestionType = b.QuestionType,
                WannaKnowId = b.WannaKnows.Where(w => w.QAUserId == qAUser_id).FirstOrDefault().Id,
                WannaKnowCount = b.WannaKnows.Count,
                QAUserThumbnailUrl = b.QAUser.ThumbnailUrl,
                QAUserId = b.QAUser.Id,
                UserName = b.QAUser.UserName,
                UpdatedAt = b.UpdatedAt,
                GenereDtos = b.Generes.Select(t => new GenereDto() { GenereId = t.Id, GenereName = t.Name }).ToList(),
                CommentDtos = b.Comments.OrderByDescending(c => c.UpdatedAt).Take(20).Select(c => new CommentDto()
                {
                    Id = c.Id,
                    Text = c.Text,
                    MovieUrl = c.Movie.MovieUrl,
                    MovieThumbnailUrl = c.Movie.MovieThumbnailUrl,
                    QAUserId = c.QAUserId,
                    UserName = c.QAUser.UserName,
                    QAUserBelongsTo = c.QAUser.BelongsTo,
                    QAUserThumbnailUrl = c.QAUser.ThumbnailUrl,
                    CommentCreatedAt = c.CreatedAt,
                    Likes = c.Likes.Count,
                    LikeId = c.Likes.Where(x => x.QAUserId == qAUser_id).FirstOrDefault().Id,
                    UpdatedAt = c.UpdatedAt,
                    QABundleId = c.QABundleId
                }).ToList(),
                TeamDto = new TeamDto()
                {
                    Id = b.TeamId,
                    Name = b.Team.Name,
                    FollowedDtos = b.Team.QAUsers.Select(u => new FollowedDto()
                    {
                        Id = u.Id,
                        UserName = u.UserName,
                        ThumbnailUrl = u.ThumbnailUrl,
                    }).ToList()
                },
                BestAnswerDto = new BestAnswerDto() { Id = b.BestAnswerId, CommentId = b.BestAnswer.CommentId }

1 个答案:

答案 0 :(得分:1)

它的类型是Expression<Func<QABudle, QABundleWithCommentDto>>所以如果你写:

Expression<Func<QABudle, QABundleWithCommentDto>> selector = b => new
{
//rest of the code
}

现在您可以使用这样的选择器:.Select(selector)