1

我有两个查询需要在 LINQ 中合并,它们实际上都来自同一个表。这样做的原因是,其中一个查询需要获取每天字段的最大值,然后将这些天数相加,而第二个查询可以立即汇总所有内容。这是第一个查询:

var queryDownload = from p in
                        (from p in almdbContext.cl_contact_event
                         where p.time_of_contact >= startDate && p.time_of_contact < endDate && p.input_file_name.Contains(inputFileName) && listName.Contains(listName)
                         group p by new
                         {
                             date = EntityFunctions.CreateDateTime(p.time_of_contact.Value.Year, p.time_of_contact.Value.Month, p.time_of_contact.Value.Day, 0, 0, 0),
                             listName = p.contact_list_name
                         } into g
                         select new
                         {
                             date     = g.Key.date,
                             listName = g.Key.listName,
                             download = g.Max(a => a.total_number_of_records)
                         })
                    group p by p.listName into g
                    select new
                    {
                        listName      = g.Key,
                        totalDownload = g.Sum(a => a.download),
                    };

这是第二个:

var queryPenData = from p in almdbContext.cl_contact_event
                   where p.time_of_contact >= startDate && p.time_of_contact < endDate && p.input_file_name.Contains(inputFileName) && listName.Contains(listName)
                   group p by p.contact_list_name into g
                   select new
                   {
                       listName           = g.Key,
                       dials              = g.Sum(a => a.ov_dial_start_time != null ? 1 : 0),
                       agentConnects      = g.Sum(a => a.agent_login_name != null ? 1 : 0),
                       abandons           = g.Sum(a => a.response_status == "DAC" || a.response_status == "DAD" ? 1 : 0),
                       rightPartyContacts = g.Sum(a => a.response_status == "PTP" || a.response_status == "RPC" ? 1 : 0),
                       promiseToPays      = g.Sum(a => a.response_status == "PTP" ? 1 : 0),
                       talkTime           = g.Sum(a => EntityFunctions.DiffSeconds(a.ov_call_connected_time, a.ov_trunk_released_time)) ?? 0,
                       wrapTime           = g.Sum(a => EntityFunctions.DiffSeconds(a.ov_trunk_released_time, a.record_released_time)) ?? 0
                   };

这是将它们连接在一起的查询。

var queryJoin = from qd in queryDownload
                join qp in queryPenData
                on qd.listName equals qp.listName
                select new
                {
                    listName           = qp.listName,
                    download           = qd.totalDownload,
                    dials              = qp.dials,
                    agentConnects      = qp.agentConnects,
                    abandons           = qp.abandons,
                    rightPartyContacts = qp.rightPartyContacts,
                    promiseToPays      = qp.promiseToPays,
                    talkTime           = qp.talkTime,
                    wrapTime           = qp.wrapTime
                };

这对我来说似乎非常冗长/迂回。有没有更好的方法可以编写/处理它来缩小/简化代码?

4

1 回答 1

2

对于您的最后一个查询,您不能只做这样的事情吗?

var queryJoin = from qd in queryDownload join qpd in queryPenData on qd.listname equals qpd....

应该能够做到这一点:

var queryJoin = from qd in queryDownload
            join qp in queryPenData
            on qd.listName equals qp.listName
            select new
            {
                qp, qd
            };

据我所知,您已经在其中形成了数据,queryPenData因此没有理由将其重新分配给最终连接中的新变量。只需选择允许您遍历匿名类型的对象queryPenData。我认为...

于 2012-06-26T22:23:32.183 回答