Search code examples
c#datacontextsystem.reflection

Get value of DataContext Result column by variable name


I've been searching and playing around with GetType() for a while, but I keep getting the name of the column (which I already have) and not the value.

I'm trying to build a csv file from the results of a method (stored procedure call) in a datacontext.

I'm fine up to figuring out how to dynamically grab result.some_column_name from the result set.

using (SomeDataContext ctx = new SomeDataContext())
        {
        List<Some_MethodResult> results = ctx.Get_Some_Method(parameter1, parameter2, parameter3).ToList();

        var colnames = ctx.Mapping.MappingSource.GetModel(typeof(SomeDataContext)).GetMetaType(typeof(Get_Some_MethodResult)).DataMembers;


        foreach (Get_Some_MethodResult r in results)
        {
            foreach (var colname in colnames)
            {
                string line = "\"" + r.GetType().GetField(colname.Name).ToString() + "\",";
                sb.Append(line);
            }
         }

The above gets me the name of the field, and I'm looking for the value. GetMember() doesn't seem to get me any better results.

I'm still looking to see if I can find out the right way to dynamically refer to a column by column name, but if this is simple and just a non-often-asked question, I'm hoping someone can point me in the right direction.


Solution

  • So, the answer has a couple of parts to it. Kudos to my co-worker Tony Colich for helping me learn on this one.

    First, it's GetProperties() and not GetFields() that I should have been looking at for the column values returned by the sproc.

    Knowing that helped immensely. The new loop looks like (please ignore the colval/boolean bit):

    using (SomeDataContext ctx = new SomeDataContext())
            {
            List<Some_MethodResult> results = ctx.Get_Some_Method(parameter1, parameter2, parameter3).ToList();
    
            var colnames = ctx.Mapping.MappingSource.GetModel(typeof(SomeDataContext)).GetMetaType(typeof(Get_Some_MethodResult)).DataMembers;
    
            var props = typeof(Get_Some_MethodResult).GetProperties().Where(p => colnames.Any(n => p.Name == n.Name));
    
            foreach (Get_Some_MethodResult r in results)
            {
                foreach (var colname in colnames)
                {
                    bool firstcol = true;
                    foreach (var colname in colnames)
                    {
                        var prop = props.FirstOrDefault(p => p.Name == colname.Name);
                        if (prop != null)
                        {
                            string colval = "";
                            if (!firstcol)
                            {
                                colval = ",\"" + prop.GetValue(r, null).ToString() + "\"";
                            }
                            else
                            {
                                colval = "\"" + prop.GetValue(r, null).ToString() + "\"";
                            }
                            //var field =
                            sb.Append(colval);
                            firstcol = false;
                        }
                    }
                    sb.AppendLine();
                }
             }
    

    Now, as far as whether or not I should have done this at all, there are only so many hours to spend on a project sometimes, and this was the path of least resistance. Thanks to all who responded!