Note that there are some explanatory texts on larger screens.

plurals
  1. POStoring/restoring values from a class to/from a list of property names and values
    text
    copied!<p>I am not sure what the best and simplest way to do this, so any advice is appreciated.</p> <p>I want to get all the fields on any/all/single domain entity class and add prefix/remove prefix dynamically when calling a particular method.</p> <p>For example, I have entities such as:</p> <pre><code>public class Shop { public string TypeOfShop{get;set} public string OwnerName {get;set} public string Address {get;set} } public class Garage { public string Company {get;set} public string Name {get;set} public string Address {get;set} } </code></pre> <p>and so on...</p> <p>I want to get a list of the properties with a prefix:</p> <pre><code>public Class Simple { public class Prop { public string Name{get;set;} public string Value{get;set;} } public ICollection list = new List&lt;Prop&gt;(); //set all prop public void GetPropertiesWithPrefix(Garage mygarage, string prefix) { list.Add(new Prop{Name = prefix + "_Company", Value = mygarage.Company}); //so on... upto 50 props... } } //to get this list I can simple call the list property on the Simple class </code></pre> <p>When reading each field I am using a switch statement and setting the value.</p> <pre><code>//Note I return a collection of Prop that have new values set within the view,lets say //this is a result returned from a controller with the existing prop names and new values... public MyGarage SetValuesForGarage(MyGarage mygarage, string prefix, ICollection&lt;Prop&gt; _props) { foreach (var item in _prop) { switch(item.Name) { case prefix + "Company": mygarage.Company = item.Value; break; //so on for each property... } } } </code></pre> <p>Is there a better, simpler or more elegant way to do this with linq or otherwise?</p>
 

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