1

I would like to know if there is a better way to write this code.The main target is to let 'Select method' know which colum of our object we going to use in our query. I would like to have something like second code:

internal class Employee
{       
    public int ID { get; set; }
    public string Name { get; set; }
    public string Sex { get; set; }
    public int Age { get; set; }
}

public interface IRepository<T> where T : class
{
    void Select(string[] table);
}
public class Repository<T> : IRepository<T> where T : class
{
    public void Select(string[] table)
    {
        // Build Query
    }
}

public partial class Main
{

    public Main()
    {

        Repository<Employee> empRepository = new Repository<Employee>();

        Employee myemp = new Employee();
    
        string[] selectedColums = {nameof(myemp.ID), nameof((myemp.Sex) };
        empRepository.Select(selectedColums);
    }
}

Now in Main class i will do something like this:

public Main()
{

    Repository<Employee> empRepository = new Repository<Employee>();

    empRepository.Select(string[] selectedColums = {=>.Sex , =>.Name });
}

We Have already our Object so why we should have a new declaration of type Employee!

Thanks a lot.

Mehdi Sh.
  • 19
  • 6
  • "I would like to know if there is a better way to write this code." That's very opinion based. I'd post this on code review. Or find a way to define what is objectively "better". Faster is an example. And that's objective. – Zer0 Jan 29 '21 at 23:49
  • 1
    there is the `nameof`: `nameof(Employee.ID)`, `nameof(Employee.Name)` – xanatos Jan 29 '21 at 23:56
  • @Zer0 Agree with you. I actually have about the last line. – Mehdi Sh. Jan 29 '21 at 23:57
  • 1
    The second `Main` doesn't compile on my VS2019, C# 9.0 – xanatos Jan 29 '21 at 23:57
  • @xanatos Right, but anyway we have to write Employee myemp = new Employee();? We actualy already have the object in out generic method. – Mehdi Sh. Jan 29 '21 at 23:58
  • 1
    @MehdiSh. no, `nameof` can use the type name. In `nameof(Employe.ID)` the `Employee` is the type name. You don't need an instance of `Employee` – xanatos Jan 29 '21 at 23:59
  • In linqQuery if you use => the base object will load, this is actualy what i,m looking for . – Mehdi Sh. Jan 30 '21 at 00:02
  • @xanatos copy the Mail code to your form class – Mehdi Sh. Jan 30 '21 at 00:03
  • 1
    But you won't have the "name" of the property. Not that `myemp.ID.GetType().Name` this will give you `ID`. It will give you `Int32` – xanatos Jan 30 '21 at 00:03
  • @xanatos True, Actualy i have to use nameof(); – Mehdi Sh. Jan 30 '21 at 00:05
  • Entity Framework (and LINQ to SQL and other frameworks) use Expression Trees to "extract" the name of the properties. You can see the syntax in `Queryable.Select`. There are many examples around about how to extract the property names then. For example [this](https://stackoverflow.com/questions/671968/retrieving-property-name-from-lambda-expression) for single expressions like `x => x.Prop1`. If you want a `x => new { x.Prop1, x.Prop2 }` it becomes a little more complex. Then if you want renames it becomes even more complex: `x => new { Foo = x.Prop1, Bar = x.Prop2 }` – xanatos Jan 30 '21 at 00:05
  • [This](https://stackoverflow.com/a/49085591/613130) for the base `x => new { x.Prop1, x.Prop2 }` expression – xanatos Jan 30 '21 at 00:11

1 Answers1

1

Putting @xanatos suggestions into code, is this what you're looking for?

public Main()
{
    Repository<Employee> empRepository = new Repository<Employee>();
    string[] selectedColums = { nameof(Employee.ID), nameof(Employee.Sex) };
    empRepository.Select(selectedColums);
}

Added bonus, nameof gets evaluated at compile time. So it can help catch errors before execution:

nameof(Employee.PropertyThatDoesntExist)

The above won't even compile.

Zer0
  • 7,191
  • 1
  • 20
  • 34
  • 1
    As a bonus suggestion, change `public void Select(string[] columns)` to `public void Select(params string[] columns)` and then you can `empRepository.Select(nameof(Employee.ID), nameof(Employee.Sex));` and can still do the `string[] selectedColums = { ... }; empRepository.Select(selectedColums)` – xanatos Jan 30 '21 at 00:13