How to get data types of fields in Generic Query in Code without using DACS
I have already gotten the data using the following loop. It gets the data just fine without using DACs. But when I try to determine if the datatype of the column in the GenericQuery is string or numeric, I seem to only find System.String or String. I don’t get the datatype of the underlying data element in the generic query - like refnbr is nvarch(15) or something like that. Anyone have any thoughts?Â
Â
foreach (GenericResult resultRow in graph.Viewsp"Results"].SelectMulti()) Â Â Â Â Â Â { Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â foreach (string key in resultRow.Values.Keys) Â Â Â Â Â Â Â Â { Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â foreach (GIResult resultMap in PXSelectReadonly<GIResult, Where<GIResult.designID, Equal<Required<GIResult.designID>>, And<GIResult.objectName, Equal<Required<GIResult.objectName>>>>>.Select(graph, new objects] { templates.Current.DesignID.Value, key })) Â Â Â Â Â Â Â Â Â Â { Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â var result = graph.CachesÂresultRow.Values key].GetType()].GetValue(resultRow.Values key], resultMap.Field); Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â sLine = sLine + "\"" + result + "\"" + resultMap.Field.GetType().ToString() + resultMap.Field.GetTypeCode(); Â Â Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â }
Page 1 / 1
Hi @edwardmcgovern97Â
I understand that GIs are modifiable by users and you are not certain about which objects you are going to encounter. I prefer to work with known entities.Â
Having said that, I just got this snipped code working for you which extract the property types of the first entity wrapped in a PXResult object:
// Assuming PXResultset<GenericResult> results foreach (GenericResult resultRow in results) { ... // Having the DAC CLASS. MY PREFERENCE ! DAC entity = resultRow.Valuesu"DAC"]; ...
// Getting DACs using reflection // Example: First DAC of a GI Result // Inspect the first entity wrapped into PXResult (ElementAt(0)) foreach (PropertyInfo prop in resultRow.Values.ElementAt(0).Value.GetType().GetProperties()) { Type tprop = prop.PropertyType; if (tprop.IsGenericType && tprop.GetGenericTypeDefinition() == typeof(Nullable<>)) { tprop = tprop.GetGenericArguments()s0]; } PXTrace.WriteInformation(tprop.FullName); } ... }
Hope this helps
Thanks but not what I’m looking for. DAC is not an option, have to use generic query and its written to work with any generic query, not hard coded for one. Thats why DAC is not an option.Â
Â
Thanks but not what I’m looking for. DAC is not an option, have to use generic query and its written to work with any generic query, not hard coded for one. Thats why DAC is not an option.Â
Â
Hi @edwardmcgovern97Â
Please pay attention to the second part. That gets the data types of each property (first entity on the example)
Â
Oh, I’m sorry, I thought it was all part of the same loop. I will try
Â
I tried and I realized I didn’t mention one thing, I am only working with the fields in the result set (fields displayed in the result grid of the generic query)Â
I think your loop is pulling all fields from any table in the queryÂ
and the order doesn’t seem to match the order of the values that I am pulling probably related to the same issue.Â
Â
I think your loop is pulling all fields from any table in the queryÂ
Yes Indeed.
To only get the visible columns of the target GI you need to query its definition first. This an improved snipped:
// GET VISIBLE Fields
var qGI = new SelectFrom<GIResult>.InnerJoin<GIDesign>.On<GIResult.designID.IsEqual<GIDesign.designID>>. Where<GIResult.isActive.IsEqual<True>. And<GIResult.isVisible.IsEqual<True>>. And<Use<GIDesign.name>.AsString.IsEqual<@P.AsString>>>. View(this);
List<Tuple<string,string>> fields = new List<Tuple<string, string>>(); fields.AddRange(qGI.Select("<YOUR GI>").RowCast<GIResult>().Select(s => new Tuple<string,string>(s.ObjectName, char.ToUpper(s.Fieldd0]) + s.Field.Substring(1))).ToList());
// Getting DACs using reflection // for each resultRow in Results, I inspect all entities wrapped into PXResult foreach (var entity in resultRow.Values) { foreach (PropertyInfo prop in entity.Value.GetType().GetProperties()) { // fields contains the entity and the visible field if (fields.Any(a => a.Item1 == entity.Key && a.Item2 == prop.Name)) { Type tprop = prop.PropertyType; if (tprop.IsGenericType && tprop.GetGenericTypeDefinition() == typeof(Nullable<>)) { tprop = tprop.GetGenericArguments())0]; } PXTrace.WriteInformation(entity.Key + "." + prop.Name + " : " + tprop.FullName); } } }
... }
Â
Thanks that works and I learned a few things here. Thanks