2

I have a strongly typed class PersonExport. I initially get data into a DataTable and call the following method on the DataTable to convert it to List<PersonExport>:

public static List<T> ConvertToList<T>(DataTable dt, out string message)
    {
        message = string.Empty;
        var list = new List<T>();


        try
        {
            var columnNames = dt.Columns.Cast<DataColumn>()
            .Select(c => c.ColumnName)
            .ToList();

            var properties = typeof(T).GetProperties();

            list = dt.AsEnumerable().Select(row =>
            {
                var objT = Activator.CreateInstance<T>();

                foreach (var pro in properties)
                {
                    if (columnNames.Contains(pro.Name))
                    {
                        var value = row[pro.Name];
                        var typeName = value.GetType().FullName;

                        if (typeName == "MySql.Data.Types.MySqlDateTime")
                        {

                            var mySqlDateTime = (MySqlDateTime) value;
                            if (mySqlDateTime.IsValidDateTime)
                            {
                                value = Convert.ToDateTime(mySqlDateTime.ToString());
                                pro.SetValue(objT, value, null);
                            }
                        }
                        else
                        {
                            pro.SetValue(objT, row.IsNull(pro.Name) ? null : value, null);
                        }
                    }
                }

                return objT;
            }).ToList();

        }
        catch (Exception ex)
        {
            message = (ex.InnerException != null) ? ex.InnerException.Message : ex.Message;
        }

        return list;
    }

However, once I start removing columns from the DataTable returned, it no longer works because the columns in the DataTable don't match up with the properties in the PersonExport list.

I am eventually using the exported list here to export to excel, but it is not working since I have modified by DataTable and it can't Deserialize into a List<PersonExport>:

//Trying to get data into List<object>
List<object> persons = GetPersonExport(query, out message);
var exportData = new Dictionary<string, List<object>> { { "xldata", persons} };

//Deserialize to List<object> to export
var persons = JsonConvert.DeserializeObject<List<object>>(args["xldata"]);

The above line just returns a List of empty objects.

A few things got me thinking, but I am wondering what might be the best approach. I am using the EPPLUS library to export data to excel and it has the option of hiding columns, so would it be better to just export the whole object and hide columns you don't want, this way you avoid the anonymous type or what I can do is still get the whole object, but then convert it to a DataTable and then remove the columns? Thoughts?


This problem would be easier if you defined the displays, d0-d7 as an array of 7 bit vectors instead of as individual signals. Then you would have a signal display for example, where display(0) corresponded to what is d0 right now.

Once you have an array of values, then you can use something like a for...loop to describe the shifting of the values from one display position to another.

4

2 回答 2

2

你想要的只是:

public IEnumerable<object> GetListOfObject()
{
    foreach (var prod in TenMostExpensiveProducts().Tables[0].AsEnumerable())
    {
        yield return prod;
    }
}

或者:

TenMostExpensiveProducts().Tables[0].AsEnumerable().Select (x => x).ToList<object>()

但是你可以通过这样的 linq 让它更优雅地工作:

from prod in TenMostExpensiveProducts().Tables[0].AsEnumerable()
where prod.Field<decimal>("UnitPrice") > 62.500M
select prod

或者像这样(直接在 DataSet 上调用 AsDynamic):

TenMostExpensiveProducts().AsDynamic().Where (x => x.UnitPrice > 62.500M)

我更喜欢最后一种方法,而 is 是最灵活的。PS:别忘了连接System.Data.DataSetExtensions.dll参考

于 2013-11-03T17:42:16.553 回答
0
      List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
        Dictionary<string, object> row;
        foreach (DataRow dr in dt.Rows)
        {
            row = new Dictionary<string, object>();
            foreach (DataColumn col in dt.Columns)
            {
                row.Add(col.ColumnName, dr[col]);
            }
            rows.Add(row);
        }
        StringBuilder sbRes = new StringBuilder();
        jSon.Serialize(rows, sbRes);
        ret = sbRes.ToString();
于 2014-05-20T17:24:53.057 回答