Microsoft Developer Network > Forums Home > Archived Forums Forums > LINQ Project General > Accessing results after Dynamic.dll Query on Column Names
Ask a questionAsk a question
 

AnswerAccessing results after Dynamic.dll Query on Column Names

  • Monday, November 02, 2009 10:20 AMHubert-Associates Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,

    The following dynamic query works fine in LINQPad, but I have a problem accessing the contents of the resulting IEnumerable<TResult>. My table has many fields, and I use the dynamic query to select a specific set, but I can't figure out how to address the returned fields. The alternative I then had to resort to, was using "ExecuteQuery" ... I'd prefer to get it working with dynamic LINQ. Any tips?

    String nameX = "ubutu";
    var x = It_StdUnit_Types
        .Where(t => t.cl1 == 1000)
        .Select("new(_KID," + nameX + ")");
    x.Dump("ok, got the contents of nameX");

    but when I try to do a foreach(x...), the x will be enumerated, but I can't see/access any column names...

    Thanks!
    R


    Hubert-Associates

Answers

  • Monday, November 02, 2009 11:05 AMKristoferA - Huagati Systems Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    .Select("new(.... will project into a runtime generated type. It is not known/defined at design/compile time so you need to use reflection to access the members.

    E.g.:

    foreach (object o in x)
    {
        string nameXValue = GetPropertyValue(o, nameX);
    }
    
    ...

    public object GetPropertyValue(object obj, string propName)
    {
        System.Reflection.PropertyInfo propInfo = obj.GetType().GetProperty(propName);
        return propInfo.GetValue(obj, null);
    }
    
    

    Kristofer - Huagati Systems Co., Ltd.
    Cool tools for Linq-to-SQL and Entity Framework:
    huagati.com/dbmltools (add-in with new features for Visual Studio 2008's L2S and EF designers)
    huagati.com/L2SProfiler (Query profiler for Linq-to-SQL and LLBLGen Pro)
  • Tuesday, November 03, 2009 12:40 PMLingzhi SunMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hi Hubert,

     

    You can use the Take(1) extension method in the Dynamic.dll to retrieve the first object. 

    ===============================================================================
            public static IQueryable Take(this IQueryable source, int count) {

                if (source == null) throw new ArgumentNullException("source");

                return source.Provider.CreateQuery(

                    Expression.Call(

                        typeof(Queryable), "Take",

                        new Type[] { source.ElementType },

                        source.Expression, Expression.Constant(count)));

            }
    ===============================================================================

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

All Replies

  • Monday, November 02, 2009 11:05 AMKristoferA - Huagati Systems Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    .Select("new(.... will project into a runtime generated type. It is not known/defined at design/compile time so you need to use reflection to access the members.

    E.g.:

    foreach (object o in x)
    {
        string nameXValue = GetPropertyValue(o, nameX);
    }
    
    ...

    public object GetPropertyValue(object obj, string propName)
    {
        System.Reflection.PropertyInfo propInfo = obj.GetType().GetProperty(propName);
        return propInfo.GetValue(obj, null);
    }
    
    

    Kristofer - Huagati Systems Co., Ltd.
    Cool tools for Linq-to-SQL and Entity Framework:
    huagati.com/dbmltools (add-in with new features for Visual Studio 2008's L2S and EF designers)
    huagati.com/L2SProfiler (Query profiler for Linq-to-SQL and LLBLGen Pro)
  • Monday, November 02, 2009 7:19 PMHubert-Associates Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks. Great answer! After playing with this a bit, it works fine, but apparently there is no .First() operator with Dynamic.dll. So how can I get this to work if I only want a single/first-hit record?


    Hubert-Associates
  • Tuesday, November 03, 2009 12:40 PMLingzhi SunMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hi Hubert,

     

    You can use the Take(1) extension method in the Dynamic.dll to retrieve the first object. 

    ===============================================================================
            public static IQueryable Take(this IQueryable source, int count) {

                if (source == null) throw new ArgumentNullException("source");

                return source.Provider.CreateQuery(

                    Expression.Call(

                        typeof(Queryable), "Take",

                        new Type[] { source.ElementType },

                        source.Expression, Expression.Constant(count)));

            }
    ===============================================================================

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.