Reputation: 5944
Problem: I thought that connection.Query<myT>("Select A, C from myTable");
would only fill the selected columns and the matching fields of the underlying class / object. But it seems that this is not the case - (although the reason might be my misuse or how i serialize the result). Currently all fields of the underlying class are in serialized result.
Questions: What can i do to return only certain properties based on the select and serialize them?
Based on A look at dapper i put the following together:
public sealed class Category
{
public int Id { get; set; }
public string A { get; set; }
public string B { get; set; }
public string C { get; set; }
public DateTime DateAdded { get; set; }
public DateTime DateModified { get; set; }
}
Return an open IDbConnection
protected static IDbConnection OpenConnection()
{
IDbConnection connection = new SqlConnection(ConnectionString);
connection.Open();
return connection;
}
use the above connection with dapper in the repository class
public IEnumerable<Category> GetCategories()
{
using (IDbConnection connection = OpenConnection())
{
const string sql = " SELECT Id, A, C FROM Categories";
return connection.Query<Category>(sql);
}
}
[WebMethod]
public string GetCategoriesTreelist()
{
var repo = new CategoryRepository();
IEnumerable<Category> categories = repo.GetCategories();
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
return serializer.Serialize(categories);
}
Currently every field is serialized instead of only the selected fields from the sql statement above.
System.Web.Script.Serialization.JavaScriptSerializer()
or based on my misunderstanding / misuse?Thanks
Missing the forest for the trees: Like Marc said Dapper has no part that the serialization returns all members of the objects. This simple test shows that all members of the underlying class are serialized.
[WebMethod]
public string GetCategoriesTestObjectSerialization()
{
List<Category> categoriesList = new List<Category>(){
new Category(){Id=1, A="a1", C="foo"},
new Category(){Id=2, A="a2", C="bar"},
new Category(){Id=3, A="a3", C="baz"}
};
IEnumerable<Category> categories= from c in categoriesList
where c.Id > 0
select c;
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
return serializer.Serialize(categories);
}
Creating a simple Data Transfer Object solves the issue: IEnumerable<CategoryDTO> categories= from c in categoriesList where c.Id > 0 select c;
private class CategoryDTO
{
public int Id { get; set; }
public string A{ get; set; }
public string C{ get; set; }
}
Upvotes: 4
Views: 5424
Reputation: 1063704
This is fundamentally a serializer factor. Dapper explicitly never assigns to anything that it doesn't get at a column. However, I suspect that if you serialize a new Category()
you'll see very similar behaviour. Options:
Upvotes: 2