How to work with objects of unknown type returned from DataContext.ExecuteQuery

喜夏-厌秋 提交于 2019-11-29 05:06:31

More recently, we've written dapper, which fits the original question beautifully:

var result = connection.Query( "SELECT CustomerID,City FROM Customers");
foreach( var d in result )
{
    MessageBox.Show( String.Format( "{0}, {1}", d.CustomerID, d.City ) );        
}

It allows parameters etc, and there is a (preferred) typed API via Query<T> - but the dynamic API works fine too.

The mapping is done by inspecting the T and using reflection - and dynamic is really just a fancy word for object in this context. For now, you may have to just create the type that matches the expected layout.

You could try passing in Tuple<int,string>, but I haven't tried this, and I'm not sure it will realise to map ctor arg 0 to col 0, etc.

I use code like in the question quite a bit, and creating a meaningful stub class usually isn't a problem, especially with automatically implemented properties.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!