实体框架核心收益

约翰

我有这种方法从我的数据库返回数据:

public IList<Questions> GetAllQuestions(string orgId)
    {
        IList<Questions> questions = new List<Questions>();
        var x = (from o in _context.OrganizationProducts
                     join t in _context.Trackers on o.OrganizationId equals t.OrganizationId
                     join tq in _context.TrackerQuestions on t.TrackerId equals tq.TrackerId
                     join tra in _context.TrackerResponseAnswers on tq.TrackerQuestionId equals tra.TrackerQuestionId
                     join tqc in _context.TrackerQuestionChoices on tq.TrackerQuestionId equals tqc.TrackerQuestionId
                     join tr in _context.TrackerResponseAnswers on tq.TrackerQuestionId equals tr.TrackerQuestionId
                     where o.Organization.Name == orgId
                     
                     orderby tq.SortOrder
                     
                     select new
                     {
                         o.OrganizationId,
                         t.Name,
                         tq.QuestionText,
                         tqc.DisplayValue,
                         tqc.Value,
                         tqc.SortOrder,
                         tq.InputType
                     }).Distinct();
        foreach(var record in x)
        {
            questions.Add(new Questions { DisplayValue = record.DisplayValue, 
                                          InputType = record.InputType, 
                                          Name = record.Name, 
                                          OrganizationId = record.OrganizationId, 
                                          QuestionText = record.QuestionText, 
                                          SortOrder = record.SortOrder, 
                                          Value = record.Value });
        }
        return questions;
    }

使用相应的Questions类:

public class Questions
{
    public Guid OrganizationId { get; set; }
    public string Name { get; set; }
    public string QuestionText { get; set; }
    public string DisplayValue { get; set; }
    public string Value { get; set; }
    public int SortOrder { get; set; }
    public string InputType { get; set; }
}

这是我从此类中获得的响应(为安全起见删除了一些信息):

 {
    "organizationId": "[ID]",
    "name": "Quiz 1",
    "questionText": "Are you and your romantic partner:",
    "displayValue": "Dating",
    "value": "Dating",
    "sortOrder": 1,
    "inputType": "radio"
},
{
    "organizationId": "[ID]",
    "name": "Quiz 1",
    "questionText": "Are you and your romantic partner:",
    "displayValue": "Engaged",
    "value": "Engaged",
    "sortOrder": 2,
    "inputType": "radio"
},
{
    "organizationId": "[ID]",
    "name": "Quiz 1",
    "questionText": "Are you and your romantic partner:",
    "displayValue": "Married",
    "value": "Married",
    "sortOrder": 3,
    "inputType": "radio"
},

我想使它更像这样:

{
    "organizationId": "[ID]",
    "name": "Quiz 1",
    "questionText": "Are you and your romantic partner:",
    "displayValue": ["Engaged", "Married", "Dating"],
    "value": ["Engaged", "Married", "Dating"],
   
    "inputType": "radio"
},

或更妙的是:

{
        "organizationId": "[ID]",
        "name": "Quiz 1",
        "questionText": "Are you and your romantic partner:",
        "Answers": [
                  {
                     "DiplayValue": "Dating",
                     "value": "Dating",
                     "sortOrder": 1
                  },
                  {
                     "DiplayValue": "Engaged",
                     "value": "Engaged",
                     "sortOrder": 2
                  },
                  {
                     "DiplayValue": "Married",
                     "value": "Married",
                     "sortOrder": 2
                  },
         ],
        "inputType": "radio"
    },

所以我试图将这样的问题归类:

var groupQuestions = questions.Where(x => x.QuestionText != null).GroupBy(x => new { x.QuestionText, x.Value }).Select(g => g.ToList()).ToList();

但是它仍然没有输出。怎么做?此外,此查询的接缝速度有点慢,对性能改进有什么建议吗?

Svyatoslav Danyliv

其实没有什么特别的。但是应在客户端提供分组。

public IList<Questions> GetAllQuestions(string orgId)
{
    var query = from o in _context.OrganizationProducts
                join t in _context.Trackers on o.OrganizationId equals t.OrganizationId
                join tq in _context.TrackerQuestions on t.TrackerId equals tq.TrackerId
                join tra in _context.TrackerResponseAnswers on tq.TrackerQuestionId equals tra.TrackerQuestionId
                join tqc in _context.TrackerQuestionChoices on tq.TrackerQuestionId equals tqc.TrackerQuestionId
                join tr in _context.TrackerResponseAnswers on tq.TrackerQuestionId equals tr.TrackerQuestionId
                where o.Organization.Name == orgId
                orderby tq.SortOrder
                select new
                {
                    o.OrganizationId,
                    t.Name,
                    tq.QuestionText,
                    tqc.DisplayValue,
                    tqc.Value,
                    tqc.SortOrder,
                    tq.InputType
                };

    var enumerable = query.Distinct().AsEnumerable();

    var questionsQuery = 
       from q in enumerable
       group q by new { q.OrganizationId, q.Name, q.QuestionText, q.InputType } into g
       select new Questions
       {
           organizationId = g.Key.OrganizationId,
           name = g.Key.name,
           questionText = g.Key.QuestionText,
           Answers = g.OrderBy(a => a.SortOrder).Select(a => new Answer
           {
              DiplayValue = a.DiplayValue,
              value = a.Value,
              sortOrder = a.SortOrder
           }).ToArray()

           inputType = g.Key.InputType
       }

   var questions = questionsQuery.ToList();

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

实体框架核心ForEachAsync

来自分类Dev

实体框架核心.include()问题

来自分类Dev

实体框架核心的反向引擎

来自分类Dev

实体框架核心级联删除

来自分类Dev

实体框架(核心)-级联删除

来自分类Dev

实体框架的核心所在

来自分类Dev

实体框架核心.include()问题

来自分类Dev

渴望加载实体框架核心

来自分类Dev

实体框架核心加入并包含内部实体

来自分类Dev

使用实体框架或实体框架核心删除父子关系

来自分类Dev

检索由实体框架核心生成的SQL

来自分类Dev

带有实体框架核心的NoSQL

来自分类Dev

实体框架核心多对多未插入

来自分类Dev

实体框架核心不包含“包含”的定义

来自分类Dev

与实体框架核心的左外连接

来自分类Dev

实体框架核心-更改“ __EFMigrationsHistory”表的架构

来自分类Dev

实体框架核心(7)批量更新

来自分类Dev

实体框架核心:如何添加复合对象?

来自分类Dev

强类型的ID在实体框架的核心

来自分类Dev

实体框架核心3模拟PARTITION BY

来自分类Dev

实体框架核心组按日期范围

来自分类Dev

在实体框架核心上禁用AutoDetectChanges

来自分类Dev

实体框架核心不会更新ICollection

来自分类Dev

实体框架核心无钥匙导航问题

来自分类Dev

实体框架核心组截止日期

来自分类Dev

实体框架核心导致无法加载netstandard

来自分类Dev

实体框架核心能否返回视图模型

来自分类Dev

实体框架核心3.1.1多级继承

来自分类Dev

实体框架核心级联删除错误