LINQ : Dynamic select
Asked Answered
P

12

70

Consider we have this class :

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

}

How do I dynamically select for specify columns ? something like this :

  var list = new List<Data>();

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

Is this the only solution => Dynamic LINQ ?
Selected fields are not known at compile time. They would be specified at runtime

Penumbra answered 13/5, 2013 at 7:37 Comment(10)
Yes it is if your filter input are stringBanas
@CuongLe How? Any code sample or link would be great.Penumbra
What do you want the type of result to be?Anticlinal
@NicholasButler It have to be DataPenumbra
Maybe you'll find more answers in this post. #5163647Neckcloth
@Penumbra the result can't be of type Data, if it just have two properties. Datahas five.Venettavenezia
What's wrong with the dynamic linq library that you have referenced in the OP? I use it and it works very well.Anemophilous
@PatrickMcCurley Could you please elaborate on how you used Dynamic Linq Library for your dynamic runtime Select and return it as a typed List or IEnumerable?Diplo
Try with var result= list.Select(x => new {Field1 = Field1, Field2 = Field2});Eta
Similar question here: #18707971Hypostyle
A
86

You can do this by dynamically creating the lambda you pass to 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();
}

Then you can use it like this:

var result = list.Select( CreateNewStatement( "Field1, Field2" ) );
Anticlinal answered 13/5, 2013 at 8:31 Comment(12)
While this works for direct members, it does not work for nested values. For instance: I have a parent that has a child object, and I want to select child.name from parent. I'm playing with parsing the input string for periods and nesting calls to getproperty.Weise
What namespace is Data in from (Func<Data,Data>)?Crucifix
I suggest to declare it as Func<T,T> CreateNewStatement<T>(string fields), replace inside the function every occurrance of Data by T and use it as var result = list.Select(CreateNewStatement<Data>("Field1, Field2"));. This allows to use it for every entity, not just for Data.Eagre
How to use it with DataContext Classes?Transverse
Despite all the up-votes I do not find this dynamic at all. Your call to Select() returns a collection of Data objects which have properties you do not need. It should return a collection of dynamic objects. Check out the Dynamic LINQ library for a better implementation dynamiclinq.azurewebsites.netUnderhand
Return Func<Data,Data> with a lambda.Compile() will cause the IQueryable be executed, which could mean querying unnecessary column out of database. Change return type to Expression<Func<TDomainEntity, TDomainEntity>> and simply return lambda will solve the problem.Stokeontrent
what if we were selecting a new ViewModel not db class?Gliadin
@Stokeontrent better make return signature Func<S, T> rather than Func<T, T> because latter is often problematic in the context of EF.Hooky
If you use it as an expression to pass for for example Entity Framework Core, then remember to add .ToArray or .ToList to the bindings enumerable or it could hurt performance, as the bindings are enumerated for each object it binds (at least in my experience)Liftoff
@nicholas I had the same problem to select dynamic like above issue, when tried code above i got error System.ArgumentNullException: 'Value cannot be null. Parameter name: property' indicated in code var xOriginal = Expression.Property(xParameter, mi); any suggest ?Antonietta
@Stokeontrent it didn't solve the problem yet! it is returning all the column from the database.Memory
Agreed with @Underhand here. If result is items of type Data then they still have 5 properties, so what does this code even do? I needed to change some code which selected to an anonymous type and make it dynamic. I'm not sure about LINQ solutions but I realised building a DataTable object dynamically is ideal for my purpose.Cq
G
15

In addition for Nicholas Butler and the hint in comment of Matt(that use T for type of input class), I put an improve to Nicholas answer that generate the property of entity dynamically and the function does not need to send field as parameter.

For Use add class as below:

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

The DynamicSelectGenerator method get entity with type T, this method have optional input parameter Fields that if you want to select special field from entity send as a string such as "Field1, Field2" and if you don't send anything to method, it returns all of the fields of entity, you could use this method as below:

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

(Assume that you have a DbContext with name AppDbContext and the context have an entity with name SampleEntity)

Grandmamma answered 20/7, 2017 at 4:35 Comment(2)
how if the model source does not come from entity but comes from poco model? i tried it from poco model but still return unecessary field. any suggest?Antonietta
this returns every properties from the class, what should we do to only select the item that is in the fields?Memory
S
4

You must use reflection to get and set property value with it's name.

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

And here is GetPropValue() method

public static object GetPropValue(object src, string propName)
{
   return src.GetType().GetProperty(propName).GetValue(src, null);
}
Sachs answered 13/5, 2013 at 8:56 Comment(0)
T
3

Using Reflection and Expression bulid can do what you say. Example:

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();
Trinee answered 13/5, 2013 at 8:5 Comment(0)
H
2

I writing the method in following line for you can work with nested fields taking advantage of Nicholas Butler and Ali.

You can use this method for dynamically creating to lambda for pass to select and also works for nested fields. You can also work with IQueryable cases.

    /// <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;
    }

Usage:

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; }
}
Hankins answered 5/11, 2019 at 20:38 Comment(1)
What if you want to maintain the object structure, i.e., out.Nested.Field = in.Nested.Field1?Subaxillary
S
2

The OP mentioned Dynamic Linq library, so I'd like to lay out an explanation on its usage.

1. Dynamic Linq Built-In Select

Dynamic Linq has a built-in Select method, which can be used as follows:

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

The Downside

There's somewhat a downside using the built-in Select:

Since it's an IQueryable - not IQueryable<T> - extension method, with IQueryable as its return type, common materialization methods - like ToList or FirstOrDefault - can't be used. This is why the above example uses foreach - it's simply the only convenient way of materializing the results.

So to make things more convenient, let's support these methods.

2. Supporting Select<T> in Dynamic Linq (to enable using ToList and alike)

To support Select<T>, it needs to be added into the Dynamic Linq file. The simple steps for doing that are explained in this answer and in my comment on it.

After doing so, it can be used in the following way:

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(); 

The Downside

Arguably, this implementation introduces yet another kind of downside: By having to explicitly parameterize the Select<T> call (e.g., having to call Select<int>), we're losing the dynamic nature of the library.

Nevertheless, since we can now call any materialization Linq method, this usage may still be quite useful.

Schwarz answered 5/2, 2020 at 22:14 Comment(0)
M
2

I simplified the amazing method DynamicSelectGenerator() created by Ali and made this extension method that overload the LINQ Select() to take a column separated parameters to simplify the usage and for more readability:

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

So instead of:

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

Will be:

var query = list.Select("Field1,Field2").ToList();
Mendacious answered 23/10, 2021 at 23:19 Comment(0)
G
0

Another approach I've used is a nested ternary operator:

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

The ternary operator requires that each field be the same type, so you'll need to call .ToString() on any non-string columns.

Guttering answered 8/3, 2018 at 14:48 Comment(0)
A
0

I have generate my own class for same purpose of usage.

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

It generates dynamic select lambda for given string and also support for two level nested properties.

Example of usage is :

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();

It compiles the lambda as below:

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

You can also find my quesion and answer here :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();
}
Allison answered 9/8, 2018 at 12:41 Comment(1)
tried your code above i got error **System.ArgumentException: 'Type 'System.String' does not have a default constructor' based on code NewExpression innerObjNew = Expression.New(propertyType); any suggest?Antonietta
C
0

Thank you @morio. Your comment about Expression<Func<T, T>> is exactly what I needed to make this work.

I do not know how to perform an anonymous projection which seems like what most want. I say I want Field1 and Field2 from Data and I get back something like: new { Field1 = o.Field1, Field2 = o.Field2 };
But I have a need similar to many where I want to plot x and y values, but don't know until run time which ones they are. So rather than use an anonymous object, I create one that has the properties I want. In this case, X and Y. Here are the source and target classes:

    public class Source
    {
        public int PropertyA { get; set; }
        public double PropertyB { get; set; }
        public double PropertyC { get; set; }
    }

    public class Target
    {
        public double X { get; set; }
        public double Y { get; set; }
    }

And here is the code that does the mapping between the Source and the Target.

public static class SelectBuilder
{
    /// <summary>
    /// Creates a Func that can be used in a Linq Select statement that will map from the source items to a new target type.
    /// Typical usage pattern is that you have an Entity that has many properties, but you want to dynamically set properties
    /// on a smaller target type, AND, you don't know the mapping at compile time.
    /// For example, you have an Entity that has a year and 10 properties. You want to have time (year) as the X axis, but
    /// the user can chose any of the 10 properties to plot on the y axis. This would allow you to map one of the entity 
    /// properties to the Y value dynamically.
    /// </summary>
    /// <typeparam name="TSource">Type of the source, for example, and Entity Framework entity.</typeparam>
    /// <typeparam name="TTarget">Type of the target, a projection of a smaller number of properties than the entity has.</typeparam>
    /// <param name="propertyMappings">A list of named tuples that map the sourceProperty to the targetProperty.</param>
    /// <returns>A func that can be used inside the Select. 
    /// So if 
    /// var select = SelectBuilder.GetSelectStatement<Source, Target>(propertyMappings), then
    /// you can perform the select, 
    /// var results = items.Select(select);</returns>
    public static Expression<Func<TSource, TTarget>> GetSelectStatement<TSource, TTarget>(IEnumerable<(string sourceProperty, string targetProperty)> propertyMappings)
    {
        // Get the source parameter, "source". This will allow the statement to be "X = source.SourceA".
        // It needs to be of the source type, and the name is what will be used in the Select lambda.
        var sourceParameter = Expression.Parameter(typeof(TSource), "source");

        // Now define the ability to create a new Target type.
        var newTarget = Expression.New(typeof(TTarget));

        // Now develop the bindings or member assignments for each property.
        var bindings = new List<MemberAssignment>();
        foreach (var propertyMapping in propertyMappings)
        {
            var sourceMemberInfo = typeof(TSource).GetProperty(propertyMapping.sourceProperty);
            var targetMemberInfo = typeof(TTarget).GetProperty(propertyMapping.targetProperty);

            // This allows getting the value. Source parameter will provide the "source" part and sourceMemberInfo the property name.
            // For example, "source.SourceA".
            var sourceValue = Expression.Property(sourceParameter, sourceMemberInfo);

            // Provide conversion in the event there is not a perfect match for the type.
            // For example, if SourceA is int and the target X is double?, we need to convert from int to double?
            var convertExpression = Expression.Convert(sourceValue, targetMemberInfo.PropertyType);

            // Put together the target assignment, "X = Convert(source.SourcA, double?)" (TODO: How does the convert actually happen?)
            var targetAssignment = Expression.Bind(targetMemberInfo, convertExpression);
            bindings.Add(targetAssignment);
        }
        var memberInit = Expression.MemberInit(newTarget, bindings);
        // Here if we map SourceA to X and SourceB to Y the lambda will be:
        // {source => new Target() {X = Convert(source.SourceA, Nullable`1), Y = Convert(source.SourceB, Nullable`1)}}
        var lambda = Expression.Lambda<Func<TSource, TTarget>>(memberInit, sourceParameter);
        return lambda;//.Compile();
    }
}

And finally a unit test that works.

    [Fact(DisplayName = "GetSelectStatement works")]
    public void Test2()
    {
        // Arrange
        var source = new Source { PropertyA = 1, PropertyB = 2, PropertyC = 3 };
        var expectedX = Convert.ToDouble(source.PropertyA);
        var expectedY = Convert.ToDouble(source.PropertyB);
        var items = new List<Source> { source }.AsQueryable();
        // Let's map SourceA to X and SourceB to Y.
        var propertyMappings = new List<(string sourceProperty, string targetProperty)>
        {
            ("PropertyA", "X"), ("PropertyB", "Y")
            //(nameof(Source.PropertyA), nameof(Target.X)),
            //(nameof(Source.PropertyB), nameof(Target.Y))
        };

        // Act
        var select = SelectBuilder.GetSelectStatement<Source, Target>(propertyMappings);
        var actual = items.Select(select).First();

        // Assert
        actual.X.Should().Be(expectedX);
        actual.Y.Should().Be(expectedY);
    }

I've edited my previous answer since now I know how to convert from int to double. I've also made the unit test easier to understand.

I hope this helps others.

Cascio answered 22/4, 2022 at 23:34 Comment(2)
But this doesn't answer the question ("Selected fields are not known at compile time."). You basically reinvented (a small part of) AutoMapper.Anderson
@Gert Arnold: I am not an AutoMapper expert, but what I wanted on SQL Server was: "SELECT PropertyA AS X, PropertyB AS Y", rather than "SELECT PropertyA, PropertyB, PropertyC". This does that. The previous answers did not do that (but certainly provided valuable information I needed to get here) and I wasn't able to find out how to do that with AutoMapper. Seems it pulls all the data back then does the projection. I realize that plotting a chart thus knowing what you want to project to is a special case. This has a dramatic impact on our performance as we have many columns in the table.Cascio
H
-1

Using ExpandoObject you can build a dynamic objects or return the full object from the example below.

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

The following is an example of how to use this from your controller.

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();
    }
}
Hypostyle answered 25/6, 2020 at 18:0 Comment(0)
M
-13
var result = from g in list.AsEnumerable()
                select new {F1 = g.Field1,F2  = g.Field2};
Mcripley answered 13/5, 2013 at 7:45 Comment(3)
@ThomasLevesque what dynamic means hereMcripley
It means that the selected fields are not known at compile time. They would be specified at runtime, e.g. using a stringOrnie
It's not dynamic . Assume user wants to select columns of table and we have to generate Select based on his selection.Penumbra

© 2022 - 2024 — McMap. All rights reserved.