-1

In a C# project, I want to be able to create a function that accepts an IQuereable<User> object along with multiple lambda's expressions then converts it into a different object. However, instead of pulling all the available properties from the database, I want to pull only the properties that are provided to the function. Here is the logic that I think I need to follow

  1. Figure out what properties that I need to select
  2. Construct an IEnumerable from the IQueryable by specifying which properties that are needed.
  3. Iterate over every property that was selected and create the Student object.

In other words, if I call .ToList() on the IQuereable<User> users object, the query will select * from the databse table which pull all available columns. Instead I want to select only the properties that are passed as labda expressions.

Here is my code

public IEnumerable<Student> Make(IQuereable<User> users, Expression<Func<User, dynamic>> keyProperty, params Expression<Func<User, dynamic>>[] propertiesToSelect)
{
    var students = new List<Student>();

    // Make a distinct list of lambda's expressions that I need to select
    var props = propertiesToSelect.ToList();
    props.Add(keyProperty);

    props = props.Distinct().ToList();

    // TO DO!!! Some how, I need to only select the properties that are in **props** array insted of pulling all available properties
    var selectableUsers = users.Select(/**/).ToList();

    foreach(var user in selectableUsers)
    {
        var student = new Student();

        foreach(Expression<Func<User, object> exp in props)
        {
            var prop = GetPropertyInfo(user, exp)
            object value = prop.GetValue(user, null);
            // Set the property student property
            // Do somthing with prop.Name...
            // Do something with value...
        }

        students.Add(student);
    }

    return strudents;
}

Question How can I use LINQ to select only the list of Expressions

D Stanley
  • 149,601
  • 11
  • 178
  • 240
Junior
  • 11,602
  • 27
  • 106
  • 212
  • Is this a duplicate? https://stackoverflow.com/questions/9744055/pass-in-an-expression-to-linqs-select – Leo Bartkus Mar 04 '19 at 20:08
  • 1
    You could make the method generic and have an `Expression>` that creates an anonymous class of the properties you want and then also pass in a `Func` that will create a `Student` from them. – juharr Mar 04 '19 at 20:20
  • 1
    It would be helpfull if you post `User` and `Student` clasees. Also, does your `keyProperty` used to filter `User` collection? – Aleks Andreev Mar 04 '19 at 20:40
  • Please show an example of calling your function and the kind of lambda's you want to pass in the array. – NetMage Mar 04 '19 at 21:41

1 Answers1

0

This may be a case for using a stored procedure and dynamic SQL to build your initial query and control the contents of the SELECT statement.

The other option I see is to define a DTO object and leverage the AutoMapper Queryable Extensions and use the Explicit Expansion functionality to control the data to return.