My basic need is to get the datatypes from an anonymous type generated from a LINQ to SQL query.
I have a piece of code (cleverer than I could write, since I haven’t really delved into reflection) which returns the datatypes from an anonymous types, and works perfectly for the elements marked ‘not nullable’ in the linq2sql properties. So, if I have a string it will return as System.String. However, when the element is nullable I end up with the ‘full name’ of it being:
{Name = “Nullable1" FullName = "System.Nullable1[[System.Decimal, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]”}
All I want to extract is the System.Decimal type in such a case (and in cases of strings or whatever, I’d just want System.String). I’ve looked through the properties and can’t find anything that seems to store this.
private static Dictionary<string, Type> GetFieldsForType<T>(IEnumerable<T> data)
{
object o = data.First();
var properties = o.GetType().GetProperties();
return properties.ToDictionary(property => property.Name, property => property.PropertyType);
}
The LINQ query (which I don’t think really matters here):
var theData = from r in db.tblTestEdits select new { myitem = r.textField, mydecimal = r.decimalField };
I found this link that seems to try to address a similar thing.
http://ysgitdiary.blogspot.com/2010/02/blog-post.html
Though it seems to return a “string” of what the type is rather than the actual type, which is what I need. Not sure how to convert such a thing.
Many thanks all.
1 Answer