如何使用从 DataContext.ExecuteQuery 返回的未知类型的对象

发布于 2024-10-17 23:12:45 字数 781 浏览 5 评论 0原文

因此,随着 C# 4.0 中动态关键字的出现,我希望能够找到更好的解决方案来解决选择任意列时处理 DataContext.ExecuteQuery 返回的类型的问题。

过去,我要么创建一个新类型来保存此类查询的结果,要么使用描述的方法 在这篇文章中。因此,既然我能够在 .NET 4.0 下运行一个新项目,我就考虑使用动态类型以更轻松的方式完成同样的事情。

因此,我尝试了一下:

var result = _db.ExecuteQuery<dynamic>( "SELECT CustomerID,City FROM Customers", new object[0] );
foreach( var d in result )
{
    MessageBox.Show( String.Format( "{0}, {1}", d.CustomerID, d.City ) );        
}

运行时抛出异常,因为动态对象不存在属性 CustomerID。因此,由于到目前为止我对动态关键字的经验为零(一两篇文章/博客文章,没有真正的经验),我希望这里有人可以让我知道我在这里尝试做的事情是否可能。我可能高估了 ExecuteQuery 背后的“魔力”,但我认为这可能是由于在幕后完成的属性映射而起作用。非常感谢任何帮助。

So, with the advent of the dynamic keyword in C# 4.0 I am hoping I can find a better solution to the problem of dealing with types returned by DataContext.ExecuteQuery when arbitrary columns are selected.

In the past I have either created a new type to hold the result of such a query or used the method described in this SO post. So, now that I am able to work on a new project running under .NET 4.0, I looked into using a dynamic type to accomplish the same thing in a less painful manner.

So, I gave this a shot:

var result = _db.ExecuteQuery<dynamic>( "SELECT CustomerID,City FROM Customers", new object[0] );
foreach( var d in result )
{
    MessageBox.Show( String.Format( "{0}, {1}", d.CustomerID, d.City ) );        
}

An exception is thrown at runtime because the property CustomerID does not exist for the dynamic object. So, since my experience with the dynamic keyword to this point is nil (an article/blog post or two, no real experience) I was hoping someone here could let me know if what I am trying to do here is even possible. I am probably overestimating the amount of 'magic' behind ExecuteQuery, but I thought this may work due to the property mapping done behind the scenes. Any help is much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谈下烟灰 2024-10-24 23:12:45

最近,我们编写了 dapper,它非常适合原始问题:

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

它允许参数等,并且有一个通过 Query 的(首选)类型化 API - 但 dynamic API 也可以正常工作。

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.

大姐,你呐 2024-10-24 23:12:45

映射是通过检查T并使用反射来完成的 - 而动态实际上只是对象的一个花哨词在此背景下。目前,您可能只需创建与预期布局匹配的类型。

你可以尝试传入Tuple,但我还没有尝试过这个,而且我不确定它会实现将 ctor arg 0 映射到 col 我经常

使用问题中的代码,并且创建有意义的存根类通常不是问题,特别是对于自动实现的属性。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文