I have a custom entity in a relational database that I have mapped to the CLR via a domain model. So by using the following statement, I can pull in an entity from my database into memory via a LINQ query on the domain model, like so;
var inspection = (from i in dbContext.New_testinspectionExtensionBases where i.New_testinspectionId == currentInspection select i).First();
There are properties/fields on this entity that I need access to, I need to be able to determine the property/field name as well as it's value. I want to loop through these items in memory, and write out their names and values to the console.
I tried using this approach, but couldn't figure out how to correct the syntax (Nor am I sure that GetProperties is the correct method to use, GetFields wasn't returning anything for some reason so I assumed this was the way to go) but it doesn't really matter since all i need is read access to the value;
var inspectionReportFields = inspection.GetType().GetProperties(); // I called this inspectionReportfields because the entity properties correspond to // form/report fields I'm generating from this data. foreach (var reportField in inspectionReportFields) { var value = reportField.GetValue(); Console.WriteLine(reportField.Name); Console.WriteLine(value); }
Is there an easier way to get the property/field value when utilizing a domain model like EF or openaccess? If not, am I going about it the right way? And lastly, if so, how do I fix the syntax in the value variable declaration?
Here are some sample fields/properties from the code generated by the domain model, for reference;
private int? _new_systemGauges; public virtual int? New_systemGauges { get { return this._new_systemGauges; } set { this._new_systemGauges = value; } } private int? _new_systemAlarm ; public virtual int? New_systemAlarm { get { return this._new_systemAlarm; } set { this._new_systemAlarm = value; } }
LINQ to SQL translates the queries you write into equivalent SQL queries and sends them to the server for processing. More specifically, your application uses the LINQ to SQL API to request query execution. The LINQ to SQL provider then transforms the query into SQL text and delegates execution to the ADO provider.
LINQ queries are always executed when the query variable is iterated over, not when the query variable is created. This is called deferred execution. You can also force a query to execute immediately, which is useful for caching query results.
In a LINQ query, you are always working with objects. You use the same basic coding patterns to query and transform data in XML documents, SQL databases, ADO.NET Datasets, . NET collections, and any other format for which a LINQ provider is available.
In a LINQ query, the first step is to specify the data source. In C# as in most programming languages a variable must be declared before it can be used. In a LINQ query, the from clause comes first in order to introduce the data source ( customers ) and the range variable ( cust ).
I assume that you're trying to define a general-purpose way to "dump" an object without knowing anything about its structure. If so, then you are going about things the correct way. You use reflection (GetType()
and the associated Type
class methods) to inspect the object and return its information.
The reason GetFields()
didn't return anything is that you likely did not supply the right binding flags. In particular, if you call the overload that doesn't take any parameters, you only get back public
fields; if you want private fields you need to ask for them specifically.
In your case, GetFields(BindingFlags.NonPublic)
would give you back the _new_systemGauges
and _new_systemAlarm
fields, while GetProperties() would give you back the New_systemAlarm
and New_systemAlarm
properties.
The other key element you missed is that the data you are getting back is the type metadata; it defines the structure of the class
, and not any particular instance. If you want to know what the value of a property for a specific instance is, you need to ask for that:
foreach (var prop in obj.GetType().GetProperties()) { Console.WriteLine("{0} = {1}", prop.Name, prop.GetValue(obj, null)); }
One you have one of the PropertyInfo
elements from the type's metadata, you can ask for that property value on any instance of that type. It doesn't have to be the same instance that you originally used. For example:
var objs = somelist.Where(x => x.Id == 1); foreach (var prop in objs.First().GetType().GetProperties()) { int x = 0; foreach (var obj in objs) { if (prop.PropertyType.Name.Equals("Int32")) { int val = (int)prop.GetValue(obj, null); Console.WriteLine("Obj #{0}: {1} = 0x{2:x8}", x++, prop.Name, val); } else if (prop.PropertyType.Name.Equals("Decimal")) { int val = (decimal)prop.GetValue(obj, null); Console.WriteLine("Obj #{0}: {1} = {2:c2}", x++, prop.Name, val); } else { Console.WriteLine("Obj #{0}: {1} = '{2}'", x++, prop.Name, prop.GetValue(obj, null)); } } }
Technically you should check the result of GetIndexParameters
to see if a property is indexed or not; the null
parameter to GetValue
is actually an array of index values.
To convert the value you get back you can either use typecasts, or if you want to be a bit more flexible, use the Convert class's methods. The difference is, for example, if you have a short
property, GetValue()
will return a boxed short, which you cannot then typecast as an int
; you have to unbox it to a short
first. Using Convert.ToInt32()
will perform all of the needed steps to get an int
value out of any property that is convertible to an integer.
Converting between reference types is easier since you can just use is
and as
for that; those work just like you'd expect with "reflected" property values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With