0

From a third-party program, I get an list of results, as follows:

+--------+-----------+-------------+
+  Name  + Property  + Prop. value +
+--------+-----------+-------------+
+ foo    +  prop1    +  value1     +
+--------+-----------+-------------+
+ foo    +  prop2    +  value2     +
+--------+-----------+-------------+

There is a dozen property and each of those may or may not exist.

The aim is to create .NET object from these.

Right now, I use LINQ to populate objects (sorry, VB, but it is quite straightforward):

New myClass With {
.myprop1 = (From o In query.rows where o.name = 'foo' and o.col(1) = 'prop1' select o.col(2)).FirstOrDefault
...
.myprop12 = (From o In query.rows where o.name = 'foo' and o.col(1) = 'prop12' select o.col(2)).FirstOrDefault

So I have basically the same line of code 12 times. I would like to simplify it.

I have in mind something like a Dictionary(Property, String), where I could define

myprop1 <-> prop1
....
myprop12 <-> prop12

And then loop over this. I haven't figured out how to do it. Is this possible? Is there any other way to make this kind of assignements less tedious?

Servy
  • 202,030
  • 26
  • 332
  • 449
Maxime
  • 1,245
  • 2
  • 14
  • 24
  • 1
    It is? Great for you now what is your question? :) – Rand Random Jun 07 '17 at 13:52
  • So c# tag is for... – dcg Jun 07 '17 at 13:53
  • @dcg for giving me the complete code... Honestly, there are dozens of libraries to parse a text-file based on some kind of delimiter (although I suppose in your case there are many different ones? e.g. `-` and `-+`). – MakePeaceGreatAgain Jun 07 '17 at 13:55
  • You could use a dictionary. Also, you could have your property as nullable field and set them as Nothing if there's no data. – the_lotus Jun 07 '17 at 13:58
  • You probably want to see https://stackoverflow.com/q/1653046/11683, https://stackoverflow.com/q/3305275/11683 and https://stackoverflow.com/q/7595416/11683. – GSerg Jun 07 '17 at 13:58
  • Is it possible to add a **complete** example instead of extracts of what you want to do? – default Jun 07 '17 at 14:07

1 Answers1

0

Sorry, but I'm going to have to do this in C#... (I haven't tested this, but it shouldbe close..)

myClass  foo = new myClass();

var fields = from o in query.rows 
             where o.name == "foo"
             select new {Property=o.col(1), Value = o.col(2) };

foreach(var prop in foo.GetType().GetProperties())
{
      var fld = fields.FirstOrDefault(f=>f.Property == prop.Name);
      if (fld != null)
              prop.SetValue(foo, fld.Value);
}

Note, this assumes that the property name is myClass is the same as the field name, which is apparently not the case. Mapping is best handled by a Dictionary, which you'd probably have to created manually:

var FieldByProperty = new Dictionary<string, string>();

FieldByProperty .Add["myprop1", "prop1");
FieldByProperty .Add["myprop2", "prop2");
// :

foreach(var prop in foo.GetType().GetProperties())
{
      var fldName = FieldByProperty[prop.Name]; 
      var fld = fields.FirstOrDefault(f=>f.Property == fldName);
      if (fld != null)
          prop.SetValue(foo, fld.Value);
}
James Curran
  • 101,701
  • 37
  • 181
  • 258