是否可以在Dapper中使用匿名类型?
我可以看到你如何使用动态即
connection.Query<dynamic>(blah, blah, blah)
然后可以做
.Select(p=> new { A, B ,C })
或之后的某些变化?
修改的
我想我会告诉你我目前是如何使用Dapper的。我倾向于缓存(使用InMemoryCache)数据,所以我只是在开头做一个大查询(使用Dapper超级快)然后我使用Linq在我的存储库中对它进行排序。
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Common;
using System.Linq;
using Dapper;
namespace SomeNamespace.Data
{
public class DapperDataContext : IDisposable
{
private readonly string _connectionString;
private readonly DbProviderFactory _provider;
private readonly string _providerName;
public DapperDataContext()
{
const string connectionStringName = " DataContextConnectionString";
_connectionString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
_providerName = ConfigurationManager.ConnectionStrings[connectionStringName].ProviderName;
_provider = DbProviderFactories.GetFactory(_providerName);
}
public IEnumerable<MyDataView> MyData1 { get; private set; }
public IEnumerable<MyDataView> MyData2 { get; private set; }
protected string SqlSelectMyTable1Query
{
get
{
return @"SELECT Id, A, B, C from table1Name";
}
}
protected string SqlSelectMyTable2Query
{
get
{
return @"SELECT Id, A, B, C from table2Name";
}
}
public void Dispose()
{
}
public void Refresh()
{
using (var connection = _provider.CreateConnection())
{
// blow up if null
connection.ConnectionString = _connectionString;
connection.Open();
var sql = String.Join(" ",
new[]
{
SqlSelectMyTable1Query,
SqlSelectMyTable2Query
});
using (var multi = connection.QueryMultiple(sql))
{
MyData1 = multi.Read<MyDataView>().ToList();
MyData2 = multi.Read<MyDataView>().ToList();
}
}
}
public class MyDataView
{
public long Id { get; set; }
public string A { get; set; }
public string B { get; set; }
public string C { get; set; }
}
}
}
InMemoryCache看起来像这样
namespace Libs.Web
{
public class InMemoryCache : ICacheService
{
#region ICacheService Members
public T Get<T>(string cacheId, Func<T> getItemCallback) where T : class
{
var item = HttpRuntime.Cache.Get(cacheId) as T;
if (item == null)
{
item = getItemCallback();
HttpContext.Current.Cache.Insert(cacheId, item);
}
return item;
}
public void Clear(string cacheId)
{
HttpContext.Current.Cache.Remove(cacheId);
}
#endregion
}
public interface ICacheService
{
T Get<T>(string cacheId, Func<T> getItemCallback) where T : class;
void Clear(string cacheId);
}
}
答案 0 :(得分:38)
是否可以在Dapper中使用匿名类型?
当然看到非泛型的Query覆盖,它返回一个动态IDictionary<string, object>
这个对象是一个可以使用点表示法强制转换或访问的expando。
例如:
var v = connection.Query("select 1 as a, 2 as b").First();
Console.Write("{0} {1}",v.a, v.b) // prints: 1 2
然后可以进行
.Select
当然,你得到一个IEnumerable<dynamic>
......你可以在那里运行你想要的任何东西。
答案 1 :(得分:20)
以下是使用dapper的匿名类型的另一种解决方案:
public static class DapperExtensions
{
public static IEnumerable<T> Query<T>(this IDbConnection connection, Func<T> typeBuilder, string sql)
{
return connection.Query<T>(sql);
}
}
并像这样使用它:
var data = connection.Query(() => new
{
ContactId = default(int),
Name = default(string),
}, "SELECT ContactId, Name FROM Contact");
答案 2 :(得分:3)
Guillaume86的优秀解决方案只是一个小小的改进:
public static IEnumerable<T> Query<T>(this IDbConnection connection, Func<T> typeBuilder,
string sql, dynamic param = null, IDbTransaction transaction = null,
bool buffered = true, int? commandTimeout = null, CommandType? commandType = null)
{
return SqlMapper.Query<T>(connection, sql, param, transaction, buffered,
commandTimeout, commandType);
}