使用动态表达式API选择匿名类型 [英] Select Anonymous type with Dynamic Expression API

查看:126
本文介绍了使用动态表达式API选择匿名类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用LINQ to Entities的动态表达式API( System.Linq.Dynamic )。我的LINQ查询在下面。

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

现在我有查询,我不知道如何获取每个领域的。

  string strTemp; 
foreach(查询中的var项)
{
strTemp = item。
}

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

解决方案

处理它的最简单的方法是将循环变量声明为动态,因为您没有任何静态类型信息(推断的静态类型是对象,但它是一个 string ProductDescription = item.ProductDescription;
string Name = item.Name;
}

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

  //静态(扩展)方法读取属性
public static T GetProperty< T>(此DynamicClass self,string propertyName)
{
var type = self.GetType();
var propInfo = type.GetProperty(propertyName);
return(T)propInfo.GetValue(self,null);
}

//用法:
foreach(查询中的DynamicClass项)
{
//用作扩展方法
string ProductNumber = item.GetProperty< string>(ProductNumber);
//或作为静态方法
string ProductDescription = GetProperty< string>(item,ProductDescription);
string Name = item.GetProperty< string>(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?

解决方案

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");
}

这篇关于使用动态表达式API选择匿名类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆