如何在 lambda 表达式中定义为 LINQ 选择方法中的另一个函数

本文关键字:选择 方法 函数 另一个 LINQ 定义 lambda 表达式 | 更新日期: 2023-09-27 18:34:10

我正在创建Azure Mobile App。在 api 中,我使用实体框架 6.1 创建了以下列表。问题是它在选择方法中太大了,

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 }

如何在 lambda 表达式中定义为 LINQ 选择方法中的另一个函数

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

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

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