将参数传递给Dapper List

时间:2013-12-29 01:32:47

标签: dapper

需要了解将多个参数传递给dapper list函数的最佳实践 我需要传递@StartDate,@ EndDate,@ AgentId,@ Crc传递给post方法,但不确定如何使用Dapper完成它。提前致谢

   public List<CallSearchRepositoryBO> GetCallSearchRecords(DateTime startDate, DateTime endDate, int agentId, string crc)
{
    try
    {
        using (IDbConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["CCWorkforceConnectionString"].ConnectionString))
        {


            var items = connection.Query<CallSearchRepositoryBO>
                                                      ("SELECT  h.historyid " +
                                                       " ,h.Dialid " +
                                                       " ,h.Crc " +
                                                       " ,c.szExternalId ExternalId " +
                                                       " ,a.AgentId " +
                                                       " ,UPPER(a.LastName +',' +a.FirstName) Agent" +
                                                       " ,c.ContactId" +
                                                       " ,CallDateTime" +
                                                       " ,h.ProjName Campaign" +
                                                       " ,h.PhoneNum PhoneNumber" +
                                                       " ,szQ01 Q1" +
                                                       " ,szQ02 Q2" +
                                                       " ,szQ03 Q3" +
                                                       " ,szQ04 Q4" +
                                                       " ,szQ05 Q5" +
                                                       " ,szQ06 Q6" +
                                                       " ,szQ07 Q7" +
                                                       " ,szQ08 Q8" +
                                                       " ,szQ09 Q9" +
                                                       " ,szQ10 Q10" +
                                                       " ,txtQ33 Comments" +
                                                       " ,szFaxType AlertType" +
                                                       " ,r.VoxFilePath+VoxFileName RecordingPath" +
                                                       " ,r.RecLength" +
                                                       " ,CASE WHEN DataLength(txtQ33) > 0 THEN 1 ELSE 0 END HasComments " +
                                                       " FROM Touchstar..History h" +
                                                       " INNER JOIN Touchstar..Agent a WITH(NOLOCK) ON h.AgentId = a.AgentId" +
                                                       " INNER JOIN Touchstar..Recording r WITH(NOLOCK) ON h.HistoryId = r.HistoryId" +
                                                       " LEFT JOIN Touchstar..Contact c WITH(NOLOCK)  ON h.DialId = c.DialId" +
                                                       " LEFT JOIN Touchstar..Sales s WITH(NOLOCK) ON c.ContactId = s.ContactId" +
                                                       " WHERE h.CallDateTime BETWEEN @StartDate AND @EndDate" + 
                                                       " AND h.AgentId=@AgentId"+
                                                       " AND h.Crc=@Crc," +
                                                       " AND c.szCampaignId IN('UP2','UP4')" +
                                                       " ORDER BY CallDateTime DESC").ToList();

            return items;

        }


    }
    catch (Exception)
    {

        throw;
    }

}

1 个答案:

答案 0 :(得分:2)

将对象作为第二个参数传递给Query函数。请注意以下代码中的额外参数&#34; new {StartDate = startDate,EndDate = endDate,AgentId = agentId,Crc = crc}&#34;

var items = connection.Query<CallSearchRepositoryBO>
                                                      ("SELECT  h.historyid " +
                                                       " ,h.Dialid " +
                                                       " ,h.Crc " +
                                                       " ,c.szExternalId ExternalId " +
                                                       " ,a.AgentId " +
                                                       " ,UPPER(a.LastName +',' +a.FirstName) Agent" +
                                                       " ,c.ContactId" +
                                                       " ,CallDateTime" +
                                                       " ,h.ProjName Campaign" +
                                                       " ,h.PhoneNum PhoneNumber" +
                                                       " ,szQ01 Q1" +
                                                       " ,szQ02 Q2" +
                                                       " ,szQ03 Q3" +
                                                       " ,szQ04 Q4" +
                                                       " ,szQ05 Q5" +
                                                       " ,szQ06 Q6" +
                                                       " ,szQ07 Q7" +
                                                       " ,szQ08 Q8" +
                                                       " ,szQ09 Q9" +
                                                       " ,szQ10 Q10" +
                                                       " ,txtQ33 Comments" +
                                                       " ,szFaxType AlertType" +
                                                       " ,r.VoxFilePath+VoxFileName RecordingPath" +
                                                       " ,r.RecLength" +
                                                       " ,CASE WHEN DataLength(txtQ33) > 0 THEN 1 ELSE 0 END HasComments " +
                                                       " FROM Touchstar..History h" +
                                                       " INNER JOIN Touchstar..Agent a WITH(NOLOCK) ON h.AgentId = a.AgentId" +
                                                       " INNER JOIN Touchstar..Recording r WITH(NOLOCK) ON h.HistoryId = r.HistoryId" +
                                                       " LEFT JOIN Touchstar..Contact c WITH(NOLOCK)  ON h.DialId = c.DialId" +
                                                       " LEFT JOIN Touchstar..Sales s WITH(NOLOCK) ON c.ContactId = s.ContactId" +
                                                       " WHERE h.CallDateTime BETWEEN @StartDate AND @EndDate" + 
                                                       " AND h.AgentId=@AgentId"+
                                                       " AND h.Crc=@Crc," +
                                                       " AND c.szCampaignId IN('UP2','UP4')" +
                                                       " ORDER BY CallDateTime DESC", new {StartDate = startDate, EndDate = endDate, AgentId = agentId, Crc = crc}).ToList();