Convert DataSet to List

后端 未结 11 2181
一整个雨季
一整个雨季 2020-12-04 08:39

Here is my c# code

Employee objEmp = new Employee();
List empList = new List();
foreach (DataRow dr in ds.Tables[0].Rows)
{
          


        
相关标签:
11条回答
  • 2020-12-04 09:17

    Use the code below:

    using Newtonsoft.Json;
    string JSONString = string.Empty;
    JSONString = JsonConvert.SerializeObject(ds.Tables[0]);
    
    0 讨论(0)
  • 2020-12-04 09:18

    I couldn't get Nitin Sawant's answer to work, but I was able to modify his code to work for me. Essentially I needed to use GetRuntimeFields instead of GetProperties. Here's what I ended up with:

    public static class Extensions
    {
        public static List<T> ToList<T>(this DataTable table) where T : new()
        {
            IList<FieldInfo> fields = typeof(T).GetRuntimeFields().ToList();
            List<T> result = new List<T>();
            if (row.Table.Columns.Contains(field.Name))
            {
                foreach (var row in table.Rows)
                {
                    var item = CreateItemFromRow<T>((DataRow)row, fields);
                    result.Add(item);
                }
            }
    
            return result;
        }
    
        private static T CreateItemFromRow<T>(DataRow row, IList<FieldInfo> fields) where T : new()
        {
            T item = new T();
    
            foreach (var field in fields)
            {
                if (row[field.Name] == DBNull.Value)
                    field.SetValue(item, null);
                else
                    field.SetValue(item, row[field.Name]);
            }
            return item;
        }
    }
    
    0 讨论(0)
  • 2020-12-04 09:19
    var myData = ds.Tables[0].AsEnumerable().Select(r => new Employee {
        Name = r.Field<string>("Name"),
        Age = r.Field<int>("Age")
    });
    var list = myData.ToList(); // For if you really need a List and not IEnumerable
    
    0 讨论(0)
  • 2020-12-04 09:21

    Try this....modify the code as per your needs.

          List<Employee> target = dt.AsEnumerable()
          .Select(row => new Employee
          {
            Name = row.Field<string?>(0).GetValueOrDefault(),
            Age= row.Field<int>(1)
          }).ToList();
    
    0 讨论(0)
  • 2020-12-04 09:21
     List<GSTEntity.gst_jobwork_to_mfgmaster> ListToGetJwToMfData = new List<GSTEntity.gst_jobwork_to_mfgmaster>();
                DataSet getJwtMF = new DataSet();
                getJwtMF = objgst_jobwork_to_mfgmaster_BLL.GetDataJobWorkToMfg(AssesseeId, PremiseId,  Fyear,  MonthId, out webex);
                if(getJwtMF.Tables["gst_jobwork_to_mfgmaster"] != null)
                {
    
                    ListToGetJwToMfData = (from master in getJwtMF.Tables["gst_jobwork_to_mfgmaster"].AsEnumerable() select new GSTEntity.gst_jobwork_to_mfgmaster { Partygstin = master.Field<string>("Partygstin"), Partystate =
                                           master.Field<string>("Partystate"), NatureOfTransaction = master.Field<string>("NatureOfTransaction"), ChallanNo = master.Field<string>("ChallanNo"), ChallanDate=master.Field<int>("ChallanDate"),  OtherJW_ChallanNo=master.Field<string>("OtherJW_ChallanNo"),   OtherJW_ChallanDate = master.Field<int>("OtherJW_ChallanDate"),
                        OtherJW_GSTIN=master.Field<string>("OtherJW_GSTIN"),
                        OtherJW_State = master.Field<string>("OtherJW_State"),
                        InvoiceNo = master.Field<string>("InvoiceNo"),
                        InvoiceDate=master.Field<int>("InvoiceDate"),
                        Description =master.Field<string>("Description"),
                        UQC= master.Field<string>("UQC"),
                        qty=master.Field<decimal>("qty"),
                        TaxValue=master.Field<decimal>("TaxValue"),
                        Id=master.Field<int>("Id")                        
    
                    }).ToList();
    
    0 讨论(0)
提交回复
热议问题