Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
135 views
in Technique[技术] by (71.8m points)

How to convert array collection to a dataset c#

Hi have a response class that has three models, all of a certain type...

public class AssociationResponse
{
    public AssociationModel ItemDetail {get;set;}
    public AssociationModel[] Children {get;set;}
    public AssociationModel Parent {get; set;}
    
    public int ErrorCode { get; set; }
    public string ErrorMessage { get; set; }
}

Now i am struggling in how to convert the AssociationModel[] Children to a simple dataset, so that i can convert them into datatables and show the results in a tree diagram.. unless anybody knows of a better way?

In my code behind

public AssociationModel ItemDetail { get; set; } = new AssociationModel();
public AssociationModel Parent { get; set; } = new AssociationModel();
public AssociationModel Child { get; set; } = new AssociationModel();

public async Task LoadData(string SerialNumber)
{
    try
    {
         GetAssociationBySerialNumberRequest request = new GetAssociationBySerialNumberRequest()
         {
             SerialNumber = SerialNumber
         };
    
         response = await IAssociation.AssociationGetData(request);
         AssociationResponse resp = new AssociationResponse();
         if (SerialNumber != null)
         {
             ItemDetail = response.ItemDetail;
             Parent = response.Parent;
             **DataSet dset = new DataSet();**
         }
      }

Any help would be greatful. P.S there are three tables within my [] Children.. So i wanted to somehow access them, i have tried saving to a datatable type, but that doesn't work. any help appreciated.

Edit

The problem, i am having is that i cant seem to convert the arrays to a dataset. Not sure how to do this.

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Will this work for you?

I use this extension method to convert IEnumerable to Datatables

        public static DataTable ToDataTable<T>(this IList<T> data)
        {
            var props = TypeDescriptor.GetProperties(typeof(T));
            var table = new DataTable();
            for (var i = 0; i < props.Count; i++)
            {
                var prop = props[i];
                Type propertyType;
                if (prop.PropertyType.IsGenericType &&
                    prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
                {
                    propertyType = prop.PropertyType.GetGenericArguments()[0];
                }
                else
                {
                    propertyType = prop.PropertyType;
                }

                table.Columns.Add(prop.Name, propertyType);
            }
            var values = new object[props.Count];
            foreach (var item in data)
            {
                for (var i = 0; i < values.Length; i++)
                {
                    values[i] = props[i].GetValue(item);
                }
                table.Rows.Add(values);
            }
            return table;
        }

Edited

What about this method?

public static DataTable ToDataTable(IList<AssociationModel> data)
        {
            var props = TypeDescriptor.GetProperties(typeof(AssociationModel));
            var table = new DataTable();
            for (var i = 0; i < props.Count; i++)
            {
                var prop = props[i];
                Type propertyType;
                if (prop.PropertyType.IsGenericType &&
                    prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
                {
                    propertyType = prop.PropertyType.GetGenericArguments()[0];
                }
                else
                {
                    propertyType = prop.PropertyType;
                }

                table.Columns.Add(prop.Name, propertyType);
            }
            var values = new object[props.Count];
            foreach (var item in data)
            {
                for (var i = 0; i < values.Length; i++)
                {
                    values[i] = props[i].GetValue(item);
                }
                table.Rows.Add(values);
            }
            return table;
        }

and your code will change like follow

    if (SerialNumber != null)
    {
        ItemDetail = response.ItemDetail;
        Parent = response.Parent;
        DataTable dataTable = ToDataTable(response.Children.ToList());
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...