How can I make a copy of an unknown incoming object with extra properties?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DDKelley
    New Member
    • Sep 2010
    • 2

    How can I make a copy of an unknown incoming object with extra properties?

    I have an extremely complex object being passed to my code. I do not, at compile time (or while I am programming the code, for that matter) know what the object contains, other than a few properties, and that it is an IQueryable object...

    I have to manipulate the data contained in the object by grouping on as many as 22 different datapoints (if they exist in the incoming object, and are specified in an incoming string parameter accompanying the object), aggregating the results, and returning the modified object.

    The problem: LINQ (and all other C# queries) will not allow me to pass multiple datapoints to "GroupBy", no matter how I try to manipulate the expression. Our team came up with the "solution" of creating a new string field/property in the incoming object, concatenating the individual fields/properties into this single string and using the resulting string to do the grouping.

    Maybe I'm "codeblind" , but I can't seem to create a new object of the same type (IQueryable) with that one extra property... any suggestions?
  • Airslash
    New Member
    • Nov 2007
    • 221

    #2
    I might be wrong here, but IQueryable is an interface, thus you can never create an instance of it.

    You can try making your own instances that inherit from IQueryable and see if you can cast the received object to this Interface type, or treat it as such. If the cast fails (null) then the object cannot be used as that interface. If the cast does succeed then you can unleash all functions defined by IQueryable and your inherited interface.

    Comment

    • mldisibio
      Recognized Expert New Member
      • Sep 2008
      • 191

      #3
      You might need to post some code, but your first parameter to GroupBy is going to be a Func<yourObj, Key>, so instead of trying create a new object with a new property, why don't you encapsulate the concatenation logic into the key selector.

      In other words (psuedocode)

      Code:
      string GetKey(Foo myComplex(obj)){
        string key = new StringBuilder();
        if(obj.contains(dataPointX))
           key.Append(dataPointX.ToString());
        if(obj.contains(dataPointY))
           key.Append(dataPointY.ToString());
        // ... etc for all properties, using Reflection or whatever
        return key.ToString();
      }
      so now your GroupBy looks like:

      Code:
      myComplexObj.GroupBy(obj=>GetKey(obj));
      Last edited by mldisibio; Sep 23 '10, 01:59 PM. Reason: typo

      Comment

      • DDKelley
        New Member
        • Sep 2010
        • 2

        #4
        I'll give this a shot. Thanks for your reply. My object is a pretty complex one, derived from a database, and with almost 3000 "records"/"rows". I would LOVE to be able to post code, but the product is VERY proprietary, and I'd likely be fired at the very least.

        Comment

        Working...