Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This will be difficult if your entity types do not all implement the same interface or derive from the same class. If they do, it's pretty simple:</p> <pre><code>// example base type, which your entities would need to implement public interface IApprovable { public int ID {get; set;} public string Approved_by {get; set;} public DateTime Approved_on {get; set;} } //... public void ApproveRowTable&lt;T&gt;(List&lt;int&gt; idValues) where T : IApprovable { using(var context = new SSPModel.sspEntities()) { var table = context.Set&lt;T&gt;(); var entities = table.Where(e =&gt; idValues.Contains(e.ID)); foreach(var entity in entities) { entity.Approved_by = GlobalClass.GlobalVar; entity.Approved_on = DateTime.Now; } context.SaveChanges(); } } </code></pre> <p>If your entity types do not implement a common base type, then you should modify them by creating empty partials which do implement it:</p> <pre><code>public partial class GeneralRule : IApprovable {} </code></pre> <p>If you cannot do that, then you can do something like the following. (I'm assuming <code>ID</code> is the PK, so we can use <code>Find()</code> rather than needing to build an expression:</p> <pre><code>public void ApproveTableRows(Type entityType, IEnumerable&lt;int&gt; idsToApprove) { using(var context = new SSPModel.sspEntities()) { var set = context.Set(entityType); if(set == null) throw new ArgumentException("No DbSet found with provided name", "tableSetName"); var approveByProperty = entityType.GetProperty("Approved_by"); var approveOnProperty = entityType.GetProperty("Approved_on"); if(approveByProperty == null || approveOnProperty == null) throw new InvalidOperationException("Entity type does not contain approval properties"); foreach (object id in idsToApprove) { var entityInstance = set.Find(id); approveByProperty.SetValue(entityInstance, GlobalClass.GlobalVar); approveOnProperty.SetValue(entityInstance, DateTime.Now); } context.SaveChanges(); } } </code></pre> <p>As you can see, this is less efficient, as it issues a new query for each ID rather than getting them all at once. Also, the method accepts an entity <code>Type</code> rather than a string, to avoid the need to hunt down the right property by reflection. This could be improved, but really I think you should probably update your entities to implement a shared interface.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload