[ASP.NET] Dapper Query 관련

In Dapper, the Query method has various overloads, and it returns IEnumerable. The choice of which method to use depends on your specific needs and preferences. Here are some commonly used methods:

ToArray():

csharp
Copy code
var resultArray = dbConnection.Query(“SELECT * FROM book_cate_tbl”).ToArray();
This method converts the IEnumerable result to an array.

ToList():

csharp
Copy code
var resultList = dbConnection.Query(“SELECT * FROM book_cate_tbl”).ToList();
This method converts the IEnumerable result to a List.

AsList():

csharp
Copy code
var resultAsList = dbConnection.Query(“SELECT * FROM book_cate_tbl”).AsList();
Similar to ToList(), it converts the IEnumerable result to a List.

Single():

csharp
Copy code
var singleResult = dbConnection.QuerySingle(“SELECT * FROM book_cate_tbl WHERE SomeCondition = @param”, new { param });
This method is used when you expect a single result. It throws an exception if more than one row is returned.

SingleOrDefault():

csharp
Copy code
var singleOrDefaultResult = dbConnection.QuerySingleOrDefault(“SELECT * FROM book_cate_tbl WHERE SomeCondition = @param”, new { param });
Similar to Single(), but it returns the default value if no results are found.

First():

csharp
Copy code
var firstResult = dbConnection.QueryFirst(“SELECT * FROM book_cate_tbl WHERE SomeCondition = @param”, new { param });
This method is used to get the first result. It throws an exception if no results are found.

FirstOrDefault():

csharp
Copy code
var firstOrDefaultResult = dbConnection.QueryFirstOrDefault(“SELECT * FROM book_cate_tbl WHERE SomeCondition = @param”, new { param });
Similar to First(), but it returns the default value if no results are found.

These are some of the commonly used methods. The appropriate choice depends on the specific requirements of your query and how you want to handle the results.


dbConnection.Open();

int rowTotal = dbConnection.QuerySingle<int>("SELECT COUNT(*) FROM book_cate_tbl");

string query = string.Empty;
query += "SELECT * FROM book_cate_tbl ";
query += "WHERE idx IN(10, 11) ";
query += "ORDER BY idx DESC OFFSET 1 ROW FETCH NEXT 10 ROW ONLY";

var result = dbConnection.Query<BookCate>(query).ToArray();

var resultBody = new ResultBody
{
    Total = rowTotal,
    Body = result
};

return Ok(resultBody);


//string resultAsString = string.Join(Environment.NewLine, result.Select(item => $"idx: {item.idx}"));

string resultAsString = result[0].idx.ToString();

return Content(resultAsString, "text/plain");

/*
StringBuilder resultBuilder = new StringBuilder();

foreach (var item in result)
{
    foreach (var property in typeof(BookCate).GetProperties())
    {
        var value = property.GetValue(item);
        resultBuilder.AppendLine($"{property.Name}: {value}");
    }

    resultBuilder.AppendLine(); // Add a blank line between items
}

string resultAsString = resultBuilder.ToString();

return Content(resultAsString, "text/plain");
*/





//var resultAsString = PrintObject(result);
//return Content(resultAsString, "text/plain");
//string customResultFormat = FormatResult(resultBody);
//return Content(customResultFormat, "text/plain");

private string PrintObject(object obj)
{
    if (obj == null)
    {
        return "null";
    }

    StringBuilder stringBuilder = new StringBuilder();

    stringBuilder.AppendLine($"Type: {obj.GetType().FullName}");

    foreach (var property in obj.GetType().GetProperties())
    {
        var value = property.GetValue(obj);
        stringBuilder.AppendLine($"{property.Name}: {value ?? "null"}");
    }

    return stringBuilder.ToString();
}

private string FormatResult(ResultBody resultBody)
{
    // Your custom formatting logic here
    // For example, creating a custom string representation
    return $"Custom Result: {resultBody.Body}";
    //return string.Join(", ", resultBody.Body.Select(item => item.ToString()));
    //return $"{nameof(ResultBody)} - Body: {string.Join(", ", resultBody.Body.Select(item => item.ToString()))}";
}
guest
0 Comments
Inline Feedbacks
View all comments