这个LINQ-to-Entities连接看起来过于复杂,我做错了什么?

时间:2012-06-26 21:23:31

标签: c# linq entity-framework sybase

我需要在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
                };

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

1 个答案:

答案 0 :(得分: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中的匿名类型的对象。我想......