使用 Dyanmic Linq 处理动态选择

发布于 2024-08-13 22:16:28 字数 1329 浏览 3 评论 0原文

我正在使用 Scott Guthrie 描述的动态 Linq 库 此处

Scott Guthrie 的例子很棒,我已经多次使用了动态Where 语句。

然而,现在我面临着需要使用动态选择功能的情况。 Scott Guthrie 显示了此功能的屏幕截图(在文章的最后一个屏幕截图中),但非常聪明地从未解释过它。

问题是,即使代码编译并运行,我也不知道它如何以任何有用的方式工作。也许经过反思?

这是一个示例(请记住,您必须使用 Guthrie 在上面的文章中描述的动态 Linq 库,这不是正常的 Linq System.Linq)。

在我的示例中,我有一个包含 UserId、FirstName 和 LastName 字段的 Users 表。但您使用什么数据库并不重要。这个问题很容易重现。这是我的示例代码:

首先确保顶部有此 using 语句:

    using System.Linq.Dynamic;

然后您可以运行以下代码:

using (DataClasses1DataContext dcdc = new DataClasses1DataContext())
        {
            var x = dcdc.Users.Select("new(UserId, FirstName, LastName)");
            foreach (var item in x)
            {                    

                Console.WriteLine(item.ToString());
            }
        }

正如您所看到的,它编译并运行得很好。您从数据库中取回所有记录。但是,我无法找到实际访问新匿名类型的成员的方法。

由于 Select 查询是一个字符串,因此在设计时没有类型推断。所以我不能写:

Console.WriteLine(item.UserId);

编译器不知道匿名类型项有一个名为 UserId 的成员。因此,该代码甚至不会编译(即使您在 For..Each 循环期间暂停调试器,您也会看到调试窗口看到有 UserId、FirstName 和 LastName 成员。

所以...这应该如何工作? 如何访问匿名类型的成员?

I am using the Dynamic Linq Library that Scott Guthrie describes here.

Scott Guthrie's examples are great and I have used the dynamic Where statements quite a bit.

Now, however, I am faced with a situation where I need to use the dynamic select functionality. Scott Guthrie shows a screenshot of this functionality (in the very last screenshot in the article) but very cleverly never explains it.

The problem is, even though the code compiles and runs, I don't see how it can possibly work in any useful manner. Perhaps with reflection?

Here is an example (remember, you must use the Dynamic Linq Library that Guthrie describes in the article above, this is not the normal Linq System.Linq).

In my sample here, I have a Users table with a UserId, FirstName, and LastName fields. But it really doesn't matter what database you use. The issue is very simple to reproduce. Here is my sample code:

First make sure you have this using statement on top:

    using System.Linq.Dynamic;

Then you can run the following code:

using (DataClasses1DataContext dcdc = new DataClasses1DataContext())
        {
            var x = dcdc.Users.Select("new(UserId, FirstName, LastName)");
            foreach (var item in x)
            {                    

                Console.WriteLine(item.ToString());
            }
        }

As you can see, this compiles and runs just fine. You get all your records back from the database. However, there is no way I can find to actually access the members of the new anonymous type.

Because the Select query is a string, there is no type inference at design time. So I cannot write:

Console.WriteLine(item.UserId);

The compiler has no idea that the anonymous type item has a member named UserId. So that code will not even compile (even though if you pause the debugger during the For..Each loop you will see that the debug window sees that there are UserId, FirstName and LastName members.

So... how is this supposed to work? How do you gain access to the members of the anonymous type?

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

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

发布评论

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

评论(1

暖风昔人 2024-08-20 22:16:28

它对于数据绑定来说效果很好(我怀疑这是它的预期用例),它在幕后使用反射。它也可以与 .NET 4.0 中的 dynamic 配合使用:

foreach (dynamic item in x) { 
    Console.WriteLine(item.UserId);
}

除此之外...反射或 TypeDescriptor

foreach (object item in x) {
    Console.WriteLine(item.GetType().GetProperty("UserId").GetValue(item, null));
}

It will work fine for data-binding (I suspect this is its intended use-case), which uses reflection under the hood. It will also work fine with dynamic in .NET 4.0:

foreach (dynamic item in x) { 
    Console.WriteLine(item.UserId);
}

Other than that... reflection or TypeDescriptor.

foreach (object item in x) {
    Console.WriteLine(item.GetType().GetProperty("UserId").GetValue(item, null));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文