2

Recently I asked a more general question about getting properties of model through foreign key. Now I moved a bit further but still have no idea how transform objects on the fly.

What I've got is an IEnumerable collection which I get through repository

regionRaw = unitOfWork.RegionRepository.Get(
    keyOrder: q => q.OrderBy(d => d.RegionID),
    filter: p => p.FullName.Contains(lastname) || p.ShortName.Contains(lastname),
    orderBy: jtSorting,
    includeProperties: "District, ISO31662, GOST767Region");

Further I am going to export data from this collection to Excel. So I need a select statement that gets all the fields I need.

dt = regionRaw
    .Select(x => new
    {
        ISO = x.ISO31662.GOSTName,
        DistrictName = x.District.ShortName
    })

I do not want to enumerate all the fields I need like on the top.

I am able to make a method that recognizes which of the fields have simple values and which have objects referenced through foreign key. And then that method will return a list of properties.

Now I need some way to write something like a foreach inside select. I see something like this:

dt = regionRaw
     .Select(x => new
     {
         foreach (prop in propList)
         {
             prop.PropertyName = x.GetType()
                 .GetProperty(prop.TableName)
                 .GetValue(x, null).GetType()
                 .GetProperty(prop.PropertyName)
                 .GetValue(
                     x.GetType().GetProperty(prop.TableName).GetValue(x, null),
                     null);
         }
     }

Where propList is a collection of properties that I get before.

I do totally realize that upper code is more a pseudo-code but I have no idea how to realize this in .NET.

So if you can suggest some solution for this task I will be very grateful. Or maybe you could explain that all this is a bad idea and shouldn't be realized.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
petr346
  • 25
  • 8
  • Many a time I've faced this. Eventually I found it easier to just assign the properties manually rather than falling back to using Reflection or a mapping utility like Automapper. – Oliver Feb 27 '14 at 14:33
  • I don't quite understand but, I suspect using reflection to do this dynamically on a known type is probably bad. Be explicit and get your errors at compile time. – Jodrell Feb 27 '14 at 14:39

1 Answers1

2

You wont be able to create an anonymous type with dynamic properties as anon types are created during compile and your properties are created during execution.
But why do you need strongly typed properties if you're not going to code against them, as you wont know them until someone executes the query?

Expando object may be of use to you?
http://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject.aspx

Jonas Jämtberg
  • 573
  • 2
  • 6
  • Exactly what that object was created for. Just make sure to declare it as dynamic instead of using, say, var! – Adam Sears Feb 27 '14 at 14:47
  • Honestly it is overly complicated for the use case as far as I can tell – Oliver Feb 27 '14 at 14:51
  • @AdamSears Since you need to access fields not known at compile time you actually *shouldn't* use `dynamic`, rather you'd need to use it's built in methods, or treat it as say an `IDictionary`. – Servy Feb 27 '14 at 14:52
  • @Servy: You can cast the ExpandoObject to an IDictionary even if it's declared as dynamic and enumerate through it, or use it like a dictionary. I used it for an exclusion rule system that had an unknown number of data columns pulled from various tables in a database. Worked out decently well; but that might not be what the OP wants or needs. YMMV. – Adam Sears Feb 27 '14 at 17:06
  • @AdamSears That's my point though, you wouldn't want to cast it to `dynamic`. He would need to cast it to `IDictionary`. What `dynamic` exposes wouldn't allow him to do what he wants. – Servy Feb 27 '14 at 18:10
  • Thank you, I'll try to use expando object in the way shown here [link](http://stackoverflow.com/a/3540378/3048486). Automapper's Queryable Extensions seem to be very helpful to – petr346 Feb 28 '14 at 11:36