簡體   English   中英

如何將數組集合轉換為數據集 c#

[英]How to convert array collection to a dataset c#

嗨,有一個回復 class 有三種型號,都是某種類型...

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; }
}

現在我正在努力將 AssociationModel[] Children 轉換為簡單的數據集,以便我可以將它們轉換為數據表並在樹形圖中顯示結果.. 除非有人知道更好的方法嗎?

在我后面的代碼中

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();**
         }
      }

任何幫助都會非常有用。 PS 在我的 [] Children 中有三個表。所以我想以某種方式訪問它們,我嘗試保存到數據表類型,但這不起作用。 任何幫助表示贊賞。

編輯

我遇到的問題是我似乎無法將 arrays 轉換為數據集。 不知道該怎么做。

在此處輸入圖像描述

這對你有用嗎?

我使用此擴展方法將 IEnumerable 轉換為 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;
        }

已編輯

這個方法怎么樣?

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;
        }

你的代碼會改變如下

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM