mardi 9 avril 2019

How to set property value of object inside function?

I am using entity framework along with a repository pattern to interact with the database.

For simplicity I am doing something like this.

public T Update(T entity)
{
     // Update Entity
}

What I am wanting to do instead of changing the entity outside the function, I want the ability to pass in the expression to update the object with.

public T Update(T entity, ItemINeedPassedIn, Expression<Func<TDBTable, bool>> predicate)
{
     var dbEntity = await GetOneAsync(predicate); // Which fetches me the entity to change

     // Code to attach the property value to entity goes here <-- This is what I need

     // Update Entity
}

For example

Update(Customer, x => x.FirstName = "John", x => x.Id = 4);

Customer would be null which requires the lookup. That part works.

I need to update the first name of the customer to john where Id = 4. I want to pass in the expression and attach that to the dbEntity to be updated.

x => x.FirstName = "John"

should somehow become

dbEntity.FirstName = "John"

How Do I do this?





Aucun commentaire:

Enregistrer un commentaire