如何将分组添加到复杂的linq查询?

时间:2019-05-29 13:04:30

标签: c# linq-to-sql grouping

我目前有一个有效的查询,该查询从多个SQL表中提取数据以在kendo ui网格中显示。我现在的任务是向查询中添加分组,但是我无法成功添加将返回数据的分组。到目前为止,我所有的尝试都导致数据集为空。

我尝试将group by子句“将新{quotes.QuoteExpirationTerm}的新{quotes,t,s,rfqStarred,ch}分组到qgroup”中,这将导致一个空数据集。添加分组后,我也无法在查询中使用任何日期字段。

查询返回的字段超过100个,因此我删除了大多数字段,以尽量减少示例。

以下是工作查询中未进行分组的摘录:

var query = from m in db.RfqQuoteIntermediates
            from quotes in db.Quotes.Where(q => q.Id == m.QuoteId).DefaultIfEmpty()
            from t in db.SEWPRfqs.Where(r => r.RfqId == m.RfqId).DefaultIfEmpty()
            from s in db.RfqSupplementals.Where(x => x.RfqSeqNumber == t.RfqSeqNumber).DefaultIfEmpty()
            from rfqStarred in db.RfqUserStarreds.Where(rs => rs.RfqSeqNumber == t.RfqSeqNumber && rs.SalesRep == salesrep).DefaultIfEmpty()
            let ch = db.RfqsChangeHistories.Where(r => r.RfqRfiSeqNum == t.RfqSeqNumber && (r.Action == "RFQ marked 'Hot'" || r.Action == "RFQ unmarked 'Hot'" || r.Action == "Quote Close Date Change" || r.Action == "Hot Change Comment" 
            || r.Action == "New Comment" || r.Action == "New RFQ Comment" || r.Action == "New Quote Comment" || r.Action == "Close Date Change Comment" || r.Action == "Stage Change Comment")).OrderByDescending(r => r.Id).FirstOrDefault()
            where (whereSelector == "allPipelineRfqs"
            || (whereSelector == "pipelineRfqs" && (quotes.QuoteStage == "Pipeline" || quotes.QuoteStage == "Swing" || quotes.QuoteStage == "Forecast"))
            || (whereSelector == "closedWon" && quotes.QuoteStage == "Closed-Won")
            || (whereSelector == "closedLost" && quotes.QuoteStage == "Closed-Lost")
            || (whereSelector == "today" && quotes.QuoteDate.Date >= today)
            || (whereSelector == "thisWeek" && startOfWeek <= quotes.QuoteDate && quotes.QuoteDate < endOfWeek)
            || (whereSelector == "lastWeek" && startOfWeek <= quotes.QuoteDate && quotes.QuoteDate < endOfWeek))
            && (isAdmin || t.assignedTo == salesrep || t.secndAssignedTo == salesrep || t.addlAssignedTo == salesrep || agencies.Contains(t.Agency))
            && t.IsDeleted == false && t.IsInPipeline
            select new PipelineDto()
            {
                RfqId = t.RfqId,
                Contract = t.Contract,
                RfqSeqNumber = t.RfqSeqNumber,

        .... fields removed for example

                IsInPipeline = t.IsInPipeline,
                VendorRationale = t.VendorRationale,
                LastUpdated = t.LastUpdated,
                Acronym = s.AgencyAcronym,
                SubAcronym = s.SubAgencyAcronym,
                userstarred = rfqStarred != null,
                QuoteId = quotes.Id,
                QuoteNumber = quotes.QuoteNumber,
                QuoteDate = quotes.QuoteDate.ToShortDateString(),
                QuoteSalesContact = quotes.QuoteSalesContact,
                QuotedBy = quotes.QuotedBy,
                QuoteTotal = quotes.QuoteTotal,
                QuoteExpirationTerm = quotes.QuoteExpirationTerm,
                ActionInfo = s.ActionInfo,
                AssignedDate = s.ActionInfoDate,
                CommentNotificationLastViewed = db.RfqCommentNotifications.Where(r => r.RfqSequenceNum == t.RfqSeqNumber && r.SalesRep == salesrep).Select(d=>d.LastViewed).SingleOrDefault(),
                CommentActionDate = ch.ActionDate,
                CommentAction = ch.Action,
                CommentActionBy = ch.ActionBy
            };

这是到目前为止我在非工作组中通过查询得到的信息:

var query = from m in db.RfqQuoteIntermediates
            from quotes in db.Quotes.Where(q => q.Id == m.QuoteId).DefaultIfEmpty()
            from t in db.SEWPRfqs.Where(r => r.RfqId == m.RfqId).DefaultIfEmpty()
            from s in db.RfqSupplementals.Where(x => x.RfqSeqNumber == t.RfqSeqNumber).DefaultIfEmpty()
            from rfqStarred in db.RfqUserStarreds.Where(rs => rs.RfqSeqNumber == t.RfqSeqNumber && rs.SalesRep == salesrep).DefaultIfEmpty()
            let ch = db.RfqsChangeHistories.Where(r => r.RfqRfiSeqNum == t.RfqSeqNumber && (r.Action == "RFQ marked 'Hot'" || r.Action == "RFQ unmarked 'Hot'" || r.Action == "Quote Close Date Change" || r.Action == "Hot Change Comment" 
            || r.Action == "New Comment" || r.Action == "New RFQ Comment" || r.Action == "New Quote Comment" || r.Action == "Close Date Change Comment" || r.Action == "Stage Change Comment")).OrderByDescending(r => r.Id).FirstOrDefault()
            where (whereSelector == "allPipelineRfqs"
            || (whereSelector == "pipelineRfqs" && (quotes.QuoteStage == "Pipeline" || quotes.QuoteStage == "Swing" || quotes.QuoteStage == "Forecast"))
            || (whereSelector == "closedWon" && quotes.QuoteStage == "Closed-Won")
            || (whereSelector == "closedLost" && quotes.QuoteStage == "Closed-Lost")
            || (whereSelector == "today" && quotes.QuoteDate.Date >= today)
            || (whereSelector == "thisWeek" && startOfWeek <= quotes.QuoteDate && quotes.QuoteDate < endOfWeek)
            || (whereSelector == "lastWeek" && startOfWeek <= quotes.QuoteDate && quotes.QuoteDate < endOfWeek))
            && (isAdmin || t.assignedTo == salesrep || t.secndAssignedTo == salesrep || t.addlAssignedTo == salesrep || agencies.Contains(t.Agency))
            && t.IsDeleted == false && t.IsInPipeline

            group new {quotes, t, s, rfqStarred, ch} by new {quotes.QuoteExpirationTerm} into qgroup

            select new PipelineDto()
            {
                RfqId = Convert.ToInt32(qgroup.Select(g => g.t.RfqId)),
                Contract = qgroup.Select(g => g.t.Contract).ToString(),
                RfqSeqNumber = qgroup.Select(g => g.t.RfqSeqNumber).ToString(),
                //RFQ_RFIDate = Convert.ToDateTime(qgroup.Select(g=>g.t.RFQ_RFIDate),   * everything below this wile not compile it I uncomment any of the dates. 
                BidStatus = qgroup.Select(g => g.t.BidStatus).ToString(),
                ModLevel = qgroup.Select(g => g.t.ModLevel).ToString(),
                //ModDate = Convert.ToDateTime(qgroup.Select(g=>g.t.ModDate)),      * same here and next field.
                //ReplyByDate = Convert.ToDateTime(qgroup.Select(g=>g.t.ReplyByDate),

        .... the rest of the fields

            };

如何对quotes.QuoteExpirationTerm(值30、60或90天的int值)进行分组,它将返回数据,我需要在日期字段中进行哪些更改才能包含日期查询。

预期结果是按30天,60天和90天分组,但到目前为止,我只能得到空数据集。

有人可以指出正确的方向吗?

0 个答案:

没有答案