1. Daniel Richard
  2. .NET DataStore
  3. Thursday, 26 August 2021 17:25 PM UTC

Hi  I`m just starting to work whit SnapDevelop and .net DataStore,   while learning C# as well. I'm trying to get the column type from my Generic type DataStore , has anyone out there have an example on how to do this.

So far I can get the column name but not the type.

Here's is how I get the column name

 var columns = dataStore.DwMeta.Columns;

 foreach (var item in columns)
                   {                       
                             Console.WriteLine("Column ID: {0}, Column Name: {1},{2}", item.ID, item.Name);
                             
                     }

 

Thanks in advance,  Daniel

Francisco Martinez @Appeon Accepted Answer Pending Moderation
  1. Thursday, 26 August 2021 17:49 PM UTC
  2. .NET DataStore
  3. # 1

Hi Daniel,

Is there a particular reason you to retrieve the columns' names and types?
A C#-native way to do this would be using the nameof and typeof operators on the model.

For example:

var props = typeof(Model).GetProperties();
foreach (var prop in props) 
{
    var name = prop.Name;
    var type = prop.PropertyType.Name;
}

Let me know if this helps.

Regards,
Francisco

Comment
There are no comments made yet.
Daniel Richard Accepted Answer Pending Moderation
  1. Monday, 30 August 2021 18:34 PM UTC
  2. .NET DataStore
  3. # 2

Hi Francisco ,  Thanks for your reply.

 

Yes that more or less works I was able to get the Column name and Type.

The problem that I'm still having is for column of type Int? for example  Nullable types  they return   System.Nullable`1[T]

when  executing  prop.PropertyType.Name

 

The reason I am trying to get these types is I'm trying to dynamically loop through all columns so to get their value  example a foreach statement.

But I believe I need to know the data Type before I call the Getitem so that I can use the case or switch command to control my Getitem method <int> or <string> for example,  unless their is a another way  of doing this which I do not know about

Various ways to call the Getitems

from the IDatastoreBase 

  public TValue GetItem<TValue>(int row, string column, DwBuffer bufferType = DwBuffer.Primary, bool isOriginalValue = false);
  public TValue GetItem<TValue>(int row, short column, DwBuffer bufferType = DwBuffer.Primary, bool isOriginalValue = false);

From the IDataStore<T>

TValue GetItem<TValue>(int row, Func<TModel, TValue> columnSelector, DwBuffer bufferType = DwBuffer.Primary, bool isOriginalValue = false);

can the columnSelector be a numeric value example index of the model.

Thanks Daniel


 

Comment
  1. Francisco Martinez @Appeon
  2. Tuesday, 31 August 2021 13:40 PM UTC
You can get the underlying type of Nullable<T> properties like this (Following on my previous example):

if (Nullable.GetUnderlyingType(prop.PropertyType) is var uType && uType != null)

{

var nullableTypeName = uType.Name;

Console.WriteLine(nullableTypeName);

}



You mentioned you're using a Generic .NET DataStore, correct?

That means you already have a model class through which you access the .NET DataStore, you can safely retrieve the DataWindow's columns (without caring for their particular type) through the model itself. Is there a specific reason why this approach is not fit for you?



Regards,

Francisco
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.