62

考虑我们有这个类:

    public  class Data
{
    public string Field1 { get; set; }
    public string Field2 { get; set; }
    public string Field3 { get; set; }
    public string Field4 { get; set; }
    public string Field5 { get; set; }

}

如何动态选择指定列?像这样的东西:

  var list = new List<Data>();

  var result= list.Select("Field1,Field2"); // How ?

这是唯一的解决方案 =>动态 LINQ 吗?
选定的字段在编译时是未知的。它们将在运行时指定

4

11 回答 11

78

您可以通过动态创建传递给的 lambda 来做到这一点Select:

Func<Data,Data> CreateNewStatement( string fields )
{
    // input parameter "o"
    var xParameter = Expression.Parameter( typeof( Data ), "o" );

    // new statement "new Data()"
    var xNew = Expression.New( typeof( Data ) );

    // create initializers
    var bindings = fields.Split( ',' ).Select( o => o.Trim() )
        .Select( o => {

            // property "Field1"
            var mi = typeof( Data ).GetProperty( o );

            // original value "o.Field1"
            var xOriginal = Expression.Property( xParameter, mi );

            // set value "Field1 = o.Field1"
            return Expression.Bind( mi, xOriginal );
        }
    );

    // initialization "new Data { Field1 = o.Field1, Field2 = o.Field2 }"
    var xInit = Expression.MemberInit( xNew, bindings );

    // expression "o => new Data { Field1 = o.Field1, Field2 = o.Field2 }"
    var lambda = Expression.Lambda<Func<Data,Data>>( xInit, xParameter );

    // compile to Func<Data, Data>
    return lambda.Compile();
}

然后你可以像这样使用它:

var result = list.Select( CreateNewStatement( "Field1, Field2" ) );
于 2013-05-13T08:31:45.483 回答
12

除了 Nicholas Butler 和 Matt 的注释中的提示(T用于输入类的类型),我对 Nicholas 的答案进行了改进,动态生成实体的属性,并且函数不需要field作为参数发送。

对于使用添加类如下:

public static class Helpers
{
    public static Func<T, T> DynamicSelectGenerator<T>(string Fields = "")
    {
        string[] EntityFields;
        if (Fields == "")
            // get Properties of the T
            EntityFields = typeof(T).GetProperties().Select(propertyInfo => propertyInfo.Name).ToArray();
        else
            EntityFields = Fields.Split(',');

        // input parameter "o"
        var xParameter = Expression.Parameter(typeof(T), "o");

        // new statement "new Data()"
        var xNew = Expression.New(typeof(T));

        // create initializers
        var bindings = EntityFields.Select(o => o.Trim())
            .Select(o =>
            {

                // property "Field1"
                var mi = typeof(T).GetProperty(o);

                // original value "o.Field1"
                var xOriginal = Expression.Property(xParameter, mi);

                // set value "Field1 = o.Field1"
                return Expression.Bind(mi, xOriginal);
            }
        );

        // initialization "new Data { Field1 = o.Field1, Field2 = o.Field2 }"
        var xInit = Expression.MemberInit(xNew, bindings);

        // expression "o => new Data { Field1 = o.Field1, Field2 = o.Field2 }"
        var lambda = Expression.Lambda<Func<T, T>>(xInit, xParameter);

        // compile to Func<Data, Data>
        return lambda.Compile();
    }
}

DynamicSelectGenerator方法 get entity with type ,这个T方法有可选的输入参数Fields,如果你想从实体中选择特殊字段作为字符串发送"Field1, Field2",如果你不向方法发送任何内容,它返回实体的所有字段,你可以使用这种方法如下:

 using (AppDbContext db = new AppDbContext())
            {
                //select "Field1, Field2" from entity
                var result = db.SampleEntity.Select(Helpers.DynamicSelectGenerator<SampleEntity>("Field1, Field2")).ToList();

                //select all field from entity
                var result1 = db.SampleEntity.Select(Helpers.DynamicSelectGenerator<SampleEntity>()).ToList();
            }

(假设您有一个DbContextwith nameAppDbContext并且上下文有一个带有 name 的实体SampleEntity

于 2017-07-20T04:35:41.110 回答
4

您必须使用反射来获取和设置属性值及其名称。

  var result = new List<Data>();
  var data = new Data();
  var type = data.GetType();
  var fieldName = "Something";

  for (var i = 0; i < list.Count; i++)
  {
      foreach (var property in data.GetType().GetProperties())
      {
         if (property.Name == fieldName)
         {
            type.GetProperties().FirstOrDefault(n => n.Name == property.Name).SetValue(data, GetPropValue(list[i], property.Name), null);
            result.Add(data);
         }
      }
  }

这是 GetPropValue() 方法

public static object GetPropValue(object src, string propName)
{
   return src.GetType().GetProperty(propName).GetValue(src, null);
}
于 2013-05-13T08:56:05.463 回答
3

使用 Reflection 和 Expression bulid 可以做到你所说的。例子:

var list = new List<Data>();
//bulid a expression tree to create a paramter
ParameterExpression param = Expression.Parameter(typeof(Data), "d");
//bulid expression tree:data.Field1
Expression selector = Expression.Property(param,typeof(Data).GetProperty("Field1"));
Expression pred = Expression.Lambda(selector, param);
//bulid expression tree:Select(d=>d.Field1)
Expression expr = Expression.Call(typeof(Queryable), "Select",
    new Type[] { typeof(Data), typeof(string) },
    Expression.Constant(list.AsQueryable()), pred);
//create dynamic query
IQueryable<string> query = list.AsQueryable().Provider.CreateQuery<string>(expr);
var result=query.ToList();
于 2013-05-13T08:05:54.780 回答
2

我在下一行中编写了该方法,以便您可以利用 Nicholas Butler 和 Ali 使用嵌套字段。

您可以使用此方法动态创建到 lambda 以传递到select,也适用于嵌套字段。您也可以处理IQueryable案例。

    /// <param name="Fields">
    /// Format1: "Field1"
    /// Format2: "Nested1.Field1"
    /// Format3: "Field1:Field1Alias"
    /// </param>
    public static Expression<Func<T, TSelect>> DynamicSelectGenerator<T, TSelect>(params string[] Fields)
    {
        string[] EntityFields = Fields;
        if (Fields == null || Fields.Length == 0)
            // get Properties of the T
            EntityFields = typeof(T).GetProperties().Select(propertyInfo => propertyInfo.Name).ToArray();

        // input parameter "x"
        var xParameter = Expression.Parameter(typeof(T), "x");

        // new statement "new Data()"
        var xNew = Expression.New(typeof(TSelect));

        // create initializers
        var bindings = EntityFields
            .Select(x =>
            {
                string[] xFieldAlias = x.Split(":");
                string field = xFieldAlias[0];

                string[] fieldSplit = field.Split(".");
                if (fieldSplit.Length > 1)
                {
                    // original value "x.Nested.Field1"
                    Expression exp = xParameter;
                    foreach (string item in fieldSplit)
                        exp = Expression.PropertyOrField(exp, item);

                    // property "Field1"
                    PropertyInfo member2 = null;
                    if (xFieldAlias.Length > 1)
                        member2 = typeof(TSelect).GetProperty(xFieldAlias[1]);
                    else
                        member2 = typeof(T).GetProperty(fieldSplit[fieldSplit.Length - 1]);

                    // set value "Field1 = x.Nested.Field1"
                    var res = Expression.Bind(member2, exp);
                    return res;
                }
                // property "Field1"
                var mi = typeof(T).GetProperty(field);
                PropertyInfo member;
                if (xFieldAlias.Length > 1)
                    member = typeof(TSelect).GetProperty(xFieldAlias[1]);
                else member = typeof(TSelect).GetProperty(field);

                // original value "x.Field1"
                var xOriginal = Expression.Property(xParameter, mi);

                // set value "Field1 = x.Field1"
                return Expression.Bind(member, xOriginal);
            }
        );

        // initialization "new Data { Field1 = x.Field1, Field2 = x.Field2 }"
        var xInit = Expression.MemberInit(xNew, bindings);

        // expression "x => new Data { Field1 = x.Field1, Field2 = x.Field2 }"
        var lambda = Expression.Lambda<Func<T, TSelect>>(xInit, xParameter);

        return lambda;
    }

用法:

var s = DynamicSelectGenerator<SalesTeam, SalesTeamSelect>(
            "Name:SalesTeamName",
            "Employee.FullName:SalesTeamExpert"
            );

var res = _context.SalesTeam.Select(s);

public class SalesTeam
{
    public string Name {get; set; }

    public Guid EmployeeId { get; set; }
    public Employee Employee { get; set; }
}
public class SalesTeamSelect
{
    public string SalesTeamName {get; set; }
    public string SalesTeamExpert {get; set; }
}
于 2019-11-05T20:38:58.287 回答
2

OP 提到了 Dynamic Linq 库,所以我想解释一下它的用法。

1. 内置动态 LinqSelect

Dynamic Linq 有一个内置的Select方法,可以按如下方式使用:

var numbers = new List<int> { 1, 2, 3 };
var wrapped = numbers.Select(num => new { Value = num }).ToList();

// the "it" keyword functions as the lambda parameter,
// so essentialy it's like calling: numbers.Select(num => num)
var selectedNumbers = numbers.Select("it"); 

// the following is the equivalent of calling: wrapped.Select(num => num.Value)
var selectedValues = wrapped.Select("Value");

// the following is the equivalent of calling: numbers.Select(num => new { Value = num })
var selectedObjects = numbers.Select("new(it as Value)"); 

foreach (int num in selectedNumbers) Console.WriteLine(num);
foreach (int val in selectedValues) Console.WriteLine(val);
foreach (dynamic obj in selectedObjects) Console.WriteLine(obj.Value);

不足之处

使用内置有一些缺点Select

由于它是一个IQueryable- 非IQueryable<T>- 扩展方法,IQueryable作为它的返回类型,常见的实现方法 - 如ToListor FirstOrDefault- 不能使用。这就是上述示例使用的原因foreach- 它只是实现结果的唯一便捷方式。

所以为了让事情更方便,让我们支持这些方法。

2.支持Select<T>Dynamic Linq(启用使用ToList等)

要支持Select<T>,需要将其添加到 Dynamic Linq 文件中。此答案和我对此的评论中解释了执行此操作的简单步骤。

这样做之后,它可以通过以下方式使用:

var numbers = new List<int> { 1, 2, 3 };
var wrapped = numbers.Select(num => new { Value = num }).ToList();

// the following is the equivalent of calling: numbers.Select(num => num).ToList()
var selectedNumbers = numbers.Select<int>("it").ToList(); 

// the following is the equivalent of calling: wrapped.Select(num => num.Value).ToList()
var selectedValues = wrapped.Select<int>("Value").ToList();

// the following is the equivalent of calling: numbers.Select(num => new { Value = num }).ToList()
var selectedObjects = numbers.Select<object>("new(it as Value)").ToList(); 

不足之处

可以说,这种实现引入了另一种缺点:由于必须显式参数化Select<T>调用(例如,必须调用Select<int>),我们正在失去库的动态特性。

尽管如此,由于我们现在可以调用任何物化 Linq 方法,这种用法可能仍然非常有用。

于 2020-02-05T22:14:53.867 回答
1

我已经为相同的使用目的生成了自己的课程。

github要点:https ://gist.github.com/mstrYoda/663789375b0df23e2662a53bebaf2c7c

它为给定的字符串生成动态选择 lambda,还支持两级嵌套属性。

用法示例是:

class Shipment {
   // other fields...
   public Address Sender;
   public Address Recipient;
}

class Address {
    public string AddressText;
    public string CityName;
    public string CityId;
}

// in the service method
var shipmentDtos = _context.Shipments.Where(s => request.ShipmentIdList.Contains(s.Id))
                .Select(new SelectLambdaBuilder<Shipment>().CreateNewStatement(request.Fields)) // request.Fields = "Sender.CityName,Sender.CityId"
                .ToList();

它编译 lambda 如下:

s => new Shipment {
    Sender = new Address {
        CityId = s.Sender.CityId,
        CityName = s.Sender.CityName
    }
}

你也可以在这里找到我的问题和答案:c# - Dynamically generate linq select with nested properties

public class SelectLambdaBuilder<T>
{
// as a performence consideration I cached already computed type-properties
private static Dictionary<Type, PropertyInfo[]> _typePropertyInfoMappings = new Dictionary<Type, PropertyInfo[]>();
private readonly Type _typeOfBaseClass = typeof(T);

private Dictionary<string, List<string>> GetFieldMapping(string fields)
{
    var selectedFieldsMap = new Dictionary<string, List<string>>();

    foreach (var s in fields.Split(','))
    {
        var nestedFields = s.Split('.').Select(f => f.Trim()).ToArray();
        var nestedValue = nestedFields.Length > 1 ? nestedFields[1] : null;

        if (selectedFieldsMap.Keys.Any(key => key == nestedFields[0]))
        {
            selectedFieldsMap[nestedFields[0]].Add(nestedValue);
        }
        else
        {
            selectedFieldsMap.Add(nestedFields[0], new List<string> { nestedValue });
        }
    }

    return selectedFieldsMap;
}

public Func<T, T> CreateNewStatement(string fields)
{
    ParameterExpression xParameter = Expression.Parameter(_typeOfBaseClass, "s");
    NewExpression xNew = Expression.New(_typeOfBaseClass);

    var selectFields = GetFieldMapping(fields);

    var shpNestedPropertyBindings = new List<MemberAssignment>();
    foreach (var keyValuePair in selectFields)
    {
        PropertyInfo[] propertyInfos;
        if (!_typePropertyInfoMappings.TryGetValue(_typeOfBaseClass, out propertyInfos))
        {
            var properties = _typeOfBaseClass.GetProperties();
            propertyInfos = properties;
            _typePropertyInfoMappings.Add(_typeOfBaseClass, properties);
        }

        var propertyType = propertyInfos
            .FirstOrDefault(p => p.Name.ToLowerInvariant().Equals(keyValuePair.Key.ToLowerInvariant()))
            .PropertyType;

        if (propertyType.IsClass)
        {
            PropertyInfo objClassPropInfo = _typeOfBaseClass.GetProperty(keyValuePair.Key);
            MemberExpression objNestedMemberExpression = Expression.Property(xParameter, objClassPropInfo);

            NewExpression innerObjNew = Expression.New(propertyType);

            var nestedBindings = keyValuePair.Value.Select(v =>
            {
                PropertyInfo nestedObjPropInfo = propertyType.GetProperty(v);

                MemberExpression nestedOrigin2 = Expression.Property(objNestedMemberExpression, nestedObjPropInfo);
                var binding2 = Expression.Bind(nestedObjPropInfo, nestedOrigin2);

                return binding2;
            });

            MemberInitExpression nestedInit = Expression.MemberInit(innerObjNew, nestedBindings);
            shpNestedPropertyBindings.Add(Expression.Bind(objClassPropInfo, nestedInit));
        }
        else
        {
            Expression mbr = xParameter;
            mbr = Expression.PropertyOrField(mbr, keyValuePair.Key);

            PropertyInfo mi = _typeOfBaseClass.GetProperty( ((MemberExpression)mbr).Member.Name );

            var xOriginal = Expression.Property(xParameter, mi);

            shpNestedPropertyBindings.Add(Expression.Bind(mi, xOriginal));
        }
    }

    var xInit = Expression.MemberInit(xNew, shpNestedPropertyBindings);
    var lambda = Expression.Lambda<Func<T,T>>( xInit, xParameter );

    return lambda.Compile();
}
于 2018-08-09T12:41:43.347 回答
0

我使用的另一种方法是嵌套三元运算符:

string col = "Column3";
var query = table.Select(i => col == "Column1" ? i.Column1 :
                              col == "Column2" ? i.Column2 :
                              col == "Column3" ? i.Column3 :
                              col == "Column4" ? i.Column4 :
                              null);

三元运算符要求每个字段的类型相同,因此您需要在任何非字符串列上调用 .ToString() 。

于 2018-03-08T14:48:16.720 回答
0

我简化了 Ali 创建的惊人方法DynamicSelectGenerator(),并制作了这个覆盖 LINQ 的扩展方法,Select()以采用列分隔参数来简化使用并提高可读性:

public static IEnumerable<T> Select<T>(this IEnumerable<T> source, string parameters)
{
    return source.Select(DynamicSelectGenerator<T>(parameters));
}

所以而不是:

var query = list.Select(Helpers.DynamicSelectGenerator<Data>("Field1,Field2")).ToList();

将会:

var query = list.Select("Field1,Field2").ToList();
于 2021-10-23T23:19:55.057 回答
-1

使用 ExpandoObject,您可以构建动态对象或从下面的示例返回完整对象。

public object CreateShappedObject(object obj, List<string> lstFields)
{
    if (!lstFields.Any())
    {
        return obj;
    }
    else
    {
        ExpandoObject objectToReturn = new ExpandoObject();
        foreach (var field in lstFields)
        {
            var fieldValue = obj.GetType()
                .GetProperty(field, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance)
                .GetValue(obj, null);

            ((IDictionary<string, object>)objectToReturn).Add(field, fieldValue);
        }

        return objectToReturn;
    }
}

以下是如何从控制器中使用它的示例。

http://localhost:12345/api/yourapi?fields=field1,field2

public IHttpActionResult Get(string fields = null)
{
    try
    {
        List<string> lstFields = new List<string>();
        if (fields != null)
        {
            lstFields = fields.ToLower().Split(',').ToList();
        }
   
        // Custom query
        var result = db.data.Select(i => CreateShappedObject(new Data()
        , lstFields)).ToList();

        return Ok(result);

    }
    catch(Exception)
    {
        return InternalServerError();
    }
}
于 2020-06-25T18:00:17.987 回答
-13
var result = from g in list.AsEnumerable()
                select new {F1 = g.Field1,F2  = g.Field2};
于 2013-05-13T07:45:50.940 回答