小巧玲珑的和匿名类型 [英] Dapper and anonymous Types
问题描述
是否可以使用匿名类型的精致小巧的?
我可以看到如何使用动态即
connection.Query<动态>(胡说,胡说,胡说)
是它,然后可以做一个
。选择(p =>新建{A,b,C})
或之后?
的一些变化
的修改的
我想我会告诉你我是如何在目前使用小巧玲珑。我倾向于缓存,所以我只是做一个大的查询开头(这是超快速采用小巧玲珑的),那么我使用LINQ这一切在我的仓库整理(用InMemoryCache)的数据。
使用系统;
使用System.Collections.Generic;
使用System.Configuration;
使用System.Data.Common;
使用System.Linq的;
采用精致小巧;
命名空间SomeNamespace.Data
{
公共类DapperDataContext:IDisposable的
{
私人只读字符串_connectionString;
私人只读DbProviderFactory _provider;
私人只读字符串_providerName;
公共DapperDataContext()
{
常量字符串的connectionStringName =DataContextConnectionString;
_connectionString = ConfigurationManager.ConnectionStrings [的connectionStringName] .ConnectionString;
_providerName = ConfigurationManager.ConnectionStrings [的connectionStringName] .ProviderName;
_provider = DbProviderFactories.GetFactory(_providerName);
}
公开的IEnumerable<&名为myDataView GT; MyData1 {搞定;私人集; }
公开的IEnumerable<&名为myDataView GT; MyData2 {搞定;私人集; }
保护串SqlSelectMyTable1Query
{
得到
{
返回@从table1Name选ID,A,B,C;
}
}
保护串SqlSelectMyTable2Query
{
得到
{
返回@选ID, A,B,C从table2Name
}
}
公共无效的Dispose()
{
}
公共无效刷新()
{使用(VAR连接= _provider.CreateConnection())
{
//如果是null
connection.ConnectionString = _connectionString炸毁;
connection.Open();
VAR SQL =的string.join(,
新的[]
{
SqlSelectMyTable1Query,
SqlSelectMyTable2Query
});
使用(VAR多= connection.QueryMultiple(SQL))
{
MyData1 = multi.Read<名为myDataView方式>()了ToList();
MyData2 = multi.Read&所述;名为myDataView>()了ToList();
}
}
}
公共类名为myDataView
{
众长标识{搞定;组; }
公共字符串A {搞定;组; }
公共字符串B {搞定;组; }
公共字符串C {搞定;组; }
}
}
}
的
InMemoryCache看起来是这样的
命名空间Libs.Web
{
公共类InMemoryCache:ICacheService
{
#地区ICacheService会员
公众吨得到< T>(串cacheID,将Func键< T> getItemCallback)其中T:类
{
VAR项目= HttpRuntime.Cache.Get (cacheId)为T;
如果(项目== NULL)
{
项= getItemCallback();
HttpContext.Current.Cache.Insert(cacheID,将项目);
}
回报率的项目;
}
公共无效清除(字符串cacheId)
{
HttpContext.Current.Cache.Remove(cacheId);
}
#endregion
}
公共接口ICacheService
{
吨得到< T>(串cacheID,将Func键< ; T> getItemCallback)其中T:类;
无效清除(字符串cacheId);
}
}
是否可以使用匿名类型的精致小巧的?
块引用>
当然看到非通用查询重写,它的回报动态
的IDictionary<字符串对象>
此对象是既可以投或点符号访问在Expando。
例如:
VAR V = connection.Query(选择1为a,2为b)第一();
Console.Write({0} {1},VA,VB)//输出:1 2
是它然后就可以做一个
。选择
块引用>
当然,你会得到一个
的IEnumerable<动态方式>
...你可以运行你想要的任何东西。Is it possible to use anonymous types with Dapper?
I can see how you can use dynamic i.e.
connection.Query<dynamic>(blah, blah, blah)
is it then possible to do a
.Select(p=> new { A, B ,C })
or some variation of that afterwards?
Edit
I thought I'd show you how I am using Dapper at the moment. I tend to cache (using an InMemoryCache) data so I just do one big query at the beginning (which is super quick using Dapper) then I use Linq to sort it all out in my Repository.
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; } } } }
The InMemoryCache looks like this
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); } }
解决方案Is it possible to use anonymous types with Dapper?
Sure see the non-generic Query override, it return a dynamic
IDictionary<string, object>
this object is an expando that can either be cast or accessed with dot notation.Eg:
var v = connection.Query("select 1 as a, 2 as b").First(); Console.Write("{0} {1}",v.a, v.b) // prints: 1 2
is it then possible to do a
.Select
Sure, you get an
IEnumerable<dynamic>
... you can run anything you want on that.这篇关于小巧玲珑的和匿名类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!