首页 > 解决方案 > 在 Lambda 表达式 C# 中更改内部选择值

问题描述

我在 Lambda 表达式中有一个查询。我想编码一个在查询中分两步内部的属性。示例代码是

 public List<StudentCreativeQuestionListViewModel> GetCreativeQuestionsByQuestionSetId(long studentId,
            long questionSetId)
        {
            var questionList =
                UnitOfWork.StudentExamRepository.GetCreativeQuestionsByQuestionSetId(studentId, questionSetId);

            List<StudentCreativeQuestionListViewModel> encryptQuestionList = questionList.Select(q =>
            {
                q.Questions = q.Questions.Select(t =>
                {
                    t.TutorialList.Select(x =>
                    {
                        x.FileName = x.FileName.ToEncode();
                        return x;
                    });
                    return t;
                });
                return q;
            }).ToList();
            return encryptQuestionList.ToList();

        }

并且 GetCreativeQuestionsByQuestionSetId 存在于具有代码的另一层中:

  public IEnumerable<StudentCreativeQuestionListViewModel> GetCreativeQuestionsByQuestionSetId(long studentId, long questionSetId)
        {
            bool isPurchased = this.IsPurchased(studentId);
            var data = (from question in Context.Question
                join questionSetQuestion in Context.QuestionSetQuestion on question.Id equals questionSetQuestion
                    .QuestionId
                where questionSetQuestion.QuestionSetId == questionSetId && question.IsArchived == false
                select new StudentCreativeQuestionListViewModel
                {
                    Id = question.Id,
                    Name = question.Name,
                    Mark = questionSetQuestion.Mark,
                    LastUpdateDate = question.LastUpdateDate,
                    ImageUrl = question.ImageUrl,
                    Questions = Context.CreativeQuestion.Where(qa => qa.QuestionId == question.Id).AsEnumerable()
                        .Select(cq => new CreativeQuestionViewModel
                        {
                            Id = cq.Id,
                            Name = cq.Name,
                            TutorialList = (from aSuggestion in Context.AnswerSuggestion
                                join t in Context.Tutorial on aSuggestion.TutorialId equals t.Id
                                where aSuggestion.CreativeQuestionId == cq.Id &&
                                      t.TutorialType >= TutorialType.Video &&
                                      t.TutorialType <= TutorialType.Link
                                group t by t.TutorialType into grp
                                select grp.OrderBy(g => g.TutorialType).FirstOrDefault() into tutorial
                                join st in Context.StudentTutorial.Where(s => s.StudentId == studentId) on tutorial.Id equals st.TutorialId into sTutorialTemp
                                from sTutorial in sTutorialTemp.DefaultIfEmpty()
                                join topic in Context.Topic on tutorial.TopicId equals topic.Id into topicGroup
                                from tp in topicGroup.DefaultIfEmpty()
                                join chapter in Context.Chapter on tutorial.ChapterId equals chapter.Id into chapterGrp
                                from c in chapterGrp.DefaultIfEmpty()
                                join bk in Context.Bookmark on tutorial.Id equals bk.TutorialId into tempBk
                                from bookmark in tempBk.Where(t => t.StudentId == studentId).DefaultIfEmpty()
                                select new TutorialListViewModel
                                {
                                    Id = tutorial.Id,
                                    Body = tutorial.Body,
                                    Heading = tutorial.Heading,
                                    FileName = tutorial.FileName,
                                    ThumbUrl = tutorial.ThumbUrl,
                                    ChapterName = c.Name,
                                    TutorialType = tutorial.TutorialType,
                                    DurationInSecond = tutorial.DurationInSecond,
                                    TopicId = tutorial.TopicId,
                                    Sequence = tp != null ? tp.SequenceNumber:0,
                                    IsLocked = tutorial.IsLocked && !isPurchased,
                                    IsCompleted = sTutorial != null && sTutorial.IsCompleted,
                                    IsBookmark = bookmark != null
                                }).ToList()
                        }).ToList()
                }).AsEnumerable();

            return data;
        }

此处显示强制转换此查询的错误。显示的消息:无法将类型“System.Collections.Generic.IEnumerable”隐式转换为“System.Collections.Generic.List”。存在显式转换(您是否缺少演员表?)

标签: c#entity-frameworklinqlambda

解决方案


我认为您q.Questions = q.Questions.Select(t =>...在创建时所做的事情encryptedQuestionList导致了强制转换异常,因为“q.Questions”的类型为“List”,并且您试图在该查询中为其分配一个“IEnumerable”。

encryptedQuestionList查询可以这样写:

List<StudentCreativeQuestionListViewModel> encryptQuestionList = questionList.Select(q =>
            {
                q.Questions.Select(t =>
                {
                    t.TutorialList.Select(x =>
                    {
                        x.FileName = x.FileName.ToEncode();
                        return x;
                    });
                    return t;
                });
                return q;
            }).ToList();

推荐阅读