使用动态表达式 API 选择匿名类型

发布于 2024-09-29 12:48:26 字数 590 浏览 2 评论 0原文

我将动态表达式 API (System.Linq.Dynamic) 与 LINQ to Entities 结合使用。我的 LINQ 查询如下。

var query = this.db.Products.AsQueryable()
           .Where(strCondition)
           .OrderBy("ProductNumber")
           .Select("new(ProductNumber, ProductDescription, ProductCategory.Name)");

现在我有了“查询”,我不知道如何获取每个字段的值。

string strTemp;
foreach (var item in query)
{
    strTemp = item.?
}

它是匿名类型,所以我不能真正使用强类型来获取值。我能做些什么?我选择获取匿名类型字段的原因是因为我需要将 ProductCategory.Name 字段获取到结果中。有没有更好的方法使用动态表达式 API 在结果中获取 ProductCategory.Name?有人可以帮忙吗?

I'm using Dynamic Expression API (System.Linq.Dynamic) with LINQ to Entities. My LINQ query is below.

var query = this.db.Products.AsQueryable()
           .Where(strCondition)
           .OrderBy("ProductNumber")
           .Select("new(ProductNumber, ProductDescription, ProductCategory.Name)");

Now that I have the "query", I don't know how to get the value of each of the field.

string strTemp;
foreach (var item in query)
{
    strTemp = item.?
}

It's anonymous type so I can't really use strongly type to get the value. What can I do? The reason I select to get anonymous type fields is because I need to get ProductCategory.Name field into the result. Is there a better way to get ProductCategory.Name in the result with Dynamic Expression API? Can anyone help?

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

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

发布评论

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

评论(2

面犯桃花 2024-10-06 12:48:26

处理它的最简单方法是将循环变量声明为动态变量,因为您没有任何静态类型信息(推断的静态类型是对象,但它是DynamicClass 的实例)。

foreach (dynamic item in query)
{
    string ProductNumber      = item.ProductNumber;
    string ProductDescription = item.ProductDescription;
    string Name               = item.Name;
}

否则,您可以手动使用反射。

// static (extension) method to read the property
public static T GetProperty<T>(this DynamicClass self, string propertyName)
{
    var type = self.GetType();
    var propInfo = type.GetProperty(propertyName);
    return (T)propInfo.GetValue(self, null);        
}

// usage:
foreach (DynamicClass item in query)
{
    // using as an extension method
    string ProductNumber      = item.GetProperty<string>("ProductNumber");
    // or as a static method
    string ProductDescription = GetProperty<string>(item, "ProductDescription");
    string Name               = item.GetProperty<string>("Name");
}

The easiest way to deal with it is to declare your loop variable to be dynamic since you don't have any static type information (the inferred static type is object but it is an instance of a DynamicClass).

foreach (dynamic item in query)
{
    string ProductNumber      = item.ProductNumber;
    string ProductDescription = item.ProductDescription;
    string Name               = item.Name;
}

Otherwise you can use reflection manually.

// static (extension) method to read the property
public static T GetProperty<T>(this DynamicClass self, string propertyName)
{
    var type = self.GetType();
    var propInfo = type.GetProperty(propertyName);
    return (T)propInfo.GetValue(self, null);        
}

// usage:
foreach (DynamicClass item in query)
{
    // using as an extension method
    string ProductNumber      = item.GetProperty<string>("ProductNumber");
    // or as a static method
    string ProductDescription = GetProperty<string>(item, "ProductDescription");
    string Name               = item.GetProperty<string>("Name");
}
因为看清所以看轻 2024-10-06 12:48:26

只需使用您用来创建匿名对象的属性名称即可。
VS intellisense 应该能识别它们。

string strTemp;
        foreach (var item in query)
        {
            strTemp = item.ProductItem;
        }

您也可以指定自己的属性名称:

.Select("new(ProductNumber as Number, ProductDescription as Description, ProductCategory.Name as Name)")

Just use the property name you've used to create your anonymous object.
VS intellisense should pick them up.

string strTemp;
        foreach (var item in query)
        {
            strTemp = item.ProductItem;
        }

You can specify your own property names as well:

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