Note that there are some explanatory texts on larger screens.

plurals
  1. POAny way I can make this code cleaner/more efficient? (C#/LINQ-to-SQL)
    primarykey
    data
    text
    <p>I've been putting together an auditing solution for a program I am developing, where I am using LINQ for my update/insert operations. I have come up with the following solution (this is the snippet for inserting) (Note the Tables variable contains a list of all the tables that have been modified - I add these to the list manually and call this method):</p> <pre><code> BindingFlags b = BindingFlags.Instance | BindingFlags.Public; LINQDataContext dc = new LINQDataContext(); foreach (object Table in Tables) { string TableName = Table.ToString().Replace("Project.", ""); switch (TableName) { case "Job": string NewJobString = null; Job JobDetails = (Job)Table; var prpsJob = typeof(Job).GetProperties(b); foreach (var p in prpsJob) { object x = p.GetGetMethod().Invoke(JobDetails, null); x = StripDate(x); NewJobString += p.Name + ": " + x + Environment.NewLine; } Audit(JobID, NewJobString, "New Job created", SourceID, "", JobDetails.JobID); break; case "Estimation": string NewEstimationsString = null; Estimation EstimationDetails = (Estimation)Table; var prpsEstimations = typeof(Estimation).GetProperties(b); foreach (var p in prpsEstimations) { object x = p.GetGetMethod().Invoke(EstimationDetails, null); x = StripDate(x); NewEstimationsString += p.Name + ": " + x + Environment.NewLine; } Audit(JobID, NewEstimationsString, "New Estimation created", SourceID, "", EstimationDetails.EstimationID); break; </code></pre> <p>And the code goes on for each possible tablename. The code works fine, but it seems fairly inefficient - having a nearly identical block for each case. Is there a more efficient way?</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.
 

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