mercredi 17 mai 2023

C# Entity member type as field of class (EF Core and DLinq)

apologies if I'm not making much sense, I don't know if the thing I am aiming to achieve exists or not.

I am using EF Core for my backend API and I have a bunch of entities, sample below:

public class EntityOne {
    public int Id { get; set; }
    public string Name { get; set; }
    public string Age { get; set; }
    ...
}

I also have a generic pagination and filtering function that takes in a custom configuration per field:

public class FilterConfiguration
        {
            public string FieldName { get; set; } // Name of the field from frontend
            public string PropertyName { get; set; } // The corresponding field on the entity/list
            public FilterType FilterType { get; set; } // Type of filter
            public OperationType OperationType { get; set; } // Type of operation

            public FilterConfiguration(string fieldName, string propertyName, FilterType filterType, OperationType operationType)
            {
                FieldName = fieldName;
                PropertyName = propertyName;
                FilterType = filterType;
                OperationType = operationType;
            }
        }

Making use of this configuration looks something like the following:

new FilterConfiguration("ID", nameof(EntityOne.Id), FilterType.Integer, OperationType.Equals)

The above configuration can be interpreted as the user has entered a Value for the ID field on the frontend to filter by, the field on the frontend is called ID, the corresponding entity field is EntityOne.Id, the value type is integer and the operationtype is equals (in a nutshell, filter all data in said table where the id is 5). Using the nameof keyword works fine as I am able to take that and place it into a DLinq query for the data. What I was wondering is if the "nameof(EntityOne.Id)" can be replaced with just "EntityOne.Id". If possible, what will the FilterConfiguration class look like?

Tried looking for multiple solutions on the internet but coming up empty. Not sure if what i am looking for is possible.





Aucun commentaire:

Enregistrer un commentaire