GroupJoin with list返回多个结果,而不是按结果附加列表

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GroupJoin with list返回多个结果,而不是按结果附加列表相关的知识,希望对你有一定的参考价值。

我使用GroupJoin查询项目列表(父项目)和GroupJoin到项目列表(第一个子项目)到第二个列表(第二个子项目),其中fk到第一个子项目。为每个项目获取一个列表(父项目)我每个项目获得一个项目(所以我只想获得一个单项结果,每个项目列表(“父”)项目多次与一个项目(“第一个子项目”) “)结果。

我希望代码能更好地解释。

实体模型:

public class QuettaReq
{
    public QuettaReq()
    {
    }

    [Key] public int             Id   { get; set; }
    public       ApplicationUser User { get; set; }

    public Category Category { get; set; }

    public int CatId { get; set; }
    //Parameters 

    public virtual List<QuettaOffer>   QuettaOffer    { get; set; }
    public virtual List<QuoteQuestion> QuoteQuestions { get; set; }
}

public class QuoteQuestion
{
    [Key] public int QuoteQuestionId { get; set; }

    public int       QuoteId   { get; set; }
    public QuettaReq QuettaReq { get; set; }

    public string         Question { get; set; }
    public IList<Answers> Answers  { get; set; }
}

查看型号:

public class ReqestWithQA
{
    [Key] public int Id { get; set; }

    //public ApplicationUser User { get; set; }
    public string SiteUserId { get; set; }

    //properties
    public virtual IList<QAViewModel> QAViewModel { get; set; }
    public         int                QuettaReqId { get; set; }
}

public class QAViewModel
{
    public int    QuoteQuestionId { get; set; }
    public int    QuoteId         { get; set; }
    public string Question        { get; set; }

    public IList<Answers> Answers { get; set; }
}

查询:

IQueryable<ReqestWithQA> viewModel = _context.Quetta
        .Include(q => q.Category)
        .Where(d => d.OfferDate > DateTime.Now && d.CatId == suplayerCat)
        .Where(rq => !postOn.Contains(rq.Id))
        .GroupJoin(
            _context.quoteQuestions.Include(t => t.Answers),
            quetta => quetta.Id,
            qestion => qestion.QuoteId,
            (quetta, joinQestionQuetta) => new ReqestWithQA
            {
                ReqText         = quetta.ReqText,
                Qentity         = quetta.Qentity,
                CatId           = quetta.CatId,
                CatName         = quetta.Category.CatName,
                District        = quetta.District,
                ApplicationDate = quetta.ApplicationDate,
                DeliveryDate    = quetta.DeliveryDate,
                OfferDate       = quetta.OfferDate,
                TecnicalDetails = quetta.TecnicalDetails,
                Bedget          = quetta.Bedget,
                Id              = quetta.Id,
                QAViewModel = new[] {
                    joinQestionQuetta
                        .Select(q => new QAViewModel
                        {
                            Question = q.Question, //there are 6 question
                            Answers  = q.Answers,
                            QuoteId  = q.QuoteId

                        })
                        .FirstOrDefault()
                }
            });

return View(viewModel.Distinct().ToList()); //viewModel results are 6 Should be 1 with 6 Q

我认为问题是阵列,但我不知道该用什么代替:

QAViewModel = new[] {
    joinQestionQuetta
        .Select(q => new QAViewModel
答案

我不太确定你想用new [] { ….. }.FirstOrDefault()实现什么,以及如果你遇到一些EF Core查询翻译错误,但一般不尝试使用手册Join / GroupJoin,但尽可能使用导航属性。

使用您的实体和视图模型结构,看起来像查询应该是这样的:

var query = _context.Quetta
    .Include(qr => qr.Category) // Not needed, will be ignored
    .Where(qr => qr.OfferDate > DateTime.Now && qr.CatId == suplayerCat)
    .Where(qr => !postOn.Contains(qr.Id))
    .Select(qr => new ReqestWithQA
    {
        ReqText = qr.ReqText,
        Qentity = qr.Qentity,
        CatId = qr.CatId,
        CatName = qr.Category.CatName,
        District = qr.District,
        ApplicationDate = qr.ApplicationDate,
        DeliveryDate = qr.DeliveryDate,
        OfferDate = qr.OfferDate,
        TecnicalDetails = qr.TecnicalDetails,
        Bedget = qr.Bedget,
        Id = qr.Id,
        QAViewModel = qr.QuoteQuestions
            .Select(qq => new QAViewModel
            {
                QuoteQuestionId = qq.QuoteQuestionId,
                QuoteId = qq.QuoteId,
                Question = qq.Question,
                Answers = qq.Answers,
            })
            .ToList()
    });

var result = query.ToList();

集合导航属性充当GroupJoin,参考导航属性 - 如Join(内部或左外部,取决于关系是必需的还是可选的)。

以上是关于GroupJoin with list返回多个结果,而不是按结果附加列表的主要内容,如果未能解决你的问题,请参考以下文章

c# linq join,GroupJoin使用

使用一个对象打开多个线程并返回结果

mybatis 返回值 list列表 结果集只有一条数据?

Dart Future.wait 等待多个期货并返回不同类型的结果

mvc返回多个结果集,返回多个视图

List to JSON with JsonSerializer.Serializer 返回 null