从DataTable创建列表

时间:2013-08-07 07:12:33

标签: c# linq

我有一个实体如下:

public class MarketDataBEO
{
public MarketDataBEO()
{
    childDetails = new List<MarketDataBEO>();
}

public string MarketID { get; set; }
public string MarketHeirarchyID { get; set; }
public string MarketName { get; set; }
public string TotalMarketSizeCYM1GI { get; set; }

public List<MarketDataBEO> childDetails { get; set; }
}

关系在MarketID和MarketHeirarchyID之间

孩子也可以有另一个孩子清单

如何创建多级列表?

编辑:

MarketID | MarketHeirarchyID
1             NULL
2             NULL
3             2
4             2
5             8 <--
6             5
7             NULL
8             7
9             7
10            8

3 个答案:

答案 0 :(得分:1)

var dict = dt.AsEnumerable()
             .ToDictionary(
                 row => row[0].ToString(),
                 row => {
                     return new MarketDataBEO()
                     {
                         MarketID = row[0].ToString(),
                         MarketHeirarchyID = row[1].ToString()
                         // Other class members here
                         // MarketName = row[2].ToString()
                         // TotalMarketSizeCYM1GI = row[3].ToString()
                     }
             );

foreach (var m in dict.Values)
{
    if (!string.IsNullOrEmpty(m.MarketHeirarchyID))
        dict[m.MarketHeirarchyID].childDetails.Add(dict[m.MarketID]);
}

var result = dict.Values.ToList();

答案 1 :(得分:0)

的伪代码:

foreach(var x in datatable.Select(d=>d.MarketHerarchyId!="").ToList())
{
    datatable.Find(x.MarketHeirarchyId).childDetails.Add(x);
    datatable.Remove(x);
}

答案 2 :(得分:0)

试试这个:

var marketDataBEO = new MarketDataBEO() { MarketID = "0" };
var dictionary = new Dictionary<string, MarketDataBEO>();
dt.Rows
    .Cast<DataRow>()
    .OrderBy(dr => Convert.ToInt32(dr["MarketID"].ToString()))
    .ToList().ForEach(dr =>
    {
        var m = new MarketDataBEO()
            {
                MarketID = dr["MarketID"].ToString(),
                MarketHeirarchyID = dr["MarketHeirarchyID"].ToString()
            };
        if (dr["MarketHeirarchyID"].ToString() == "")
        {
            marketDataBEO.childDetails.Add(m);
        }
        else
        {
            dictionary[dr["MarketHeirarchyID"].ToString()].childDetails.Add(m);
        }
        dictionary.Add(m.MarketID, m);
    });

[编辑]根据您的最新信息,这是一个两步法。首先,您将获得字典中的所有行,然后您将解析KeyValuePair集合并创建您的分层对象。

var marketDataBEO = new MarketDataBEO() { MarketID = "0" };
var dictionary = new Dictionary<string, MarketDataBEO>();
dt.Rows.Cast<DataRow>()
    .ToList().ForEach(dr =>
    {
        var m = new MarketDataBEO()
            {
                MarketID = dr["MarketID"].ToString(),
                MarketHeirarchyID = dr["MarketHeirarchyID"].ToString()
            };
        dictionary.Add(m.MarketID, m);
    });
dictionary.ToList().ForEach(kvp =>
{
    if (kvp.Value.MarketHeirarchyID == "")
    {
        marketDataBEO.childDetails.Add(kvp.Value);
    }
    else
    {
        dictionary[kvp.Value.MarketHeirarchyID].childDetails.Add(kvp.Value);
    }
});