Accessing results after Dynamic.dll Query on Column Names
- 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
- .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)- Marked As Answer byHubert-Associates Monday, November 02, 2009 11:15 AM
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 SunMSDN 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.- Proposed As Answer byLingzhi SunMSFT, ModeratorMonday, November 09, 2009 12:34 AM
- Marked As Answer byLingzhi SunMSFT, ModeratorTuesday, November 10, 2009 12:53 AM
All Replies
- .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)- Marked As Answer byHubert-Associates Monday, November 02, 2009 11:15 AM
- 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 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 SunMSDN 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.- Proposed As Answer byLingzhi SunMSFT, ModeratorMonday, November 09, 2009 12:34 AM
- Marked As Answer byLingzhi SunMSFT, ModeratorTuesday, November 10, 2009 12:53 AM

