Linq表达式填充包含其他List <t>

时间:2017-03-02 16:16:12

标签: c# linq

我有一个模型(下面的示例),以及具有所需输出的样本数据。 我还给出了两种填充模型的方法,这种方法可以提供所需的输出,但是对于某些原因, 看起来我错过了什么......

无法弄清楚这里的问题是什么......

需要输入来纠正方法1或方法2,或者您也可以建议任何其他方法,以帮助在下面的响应中填充模型..

模型

class Emp
{
  public int id {get;set;}
  public int Name {get;set;}
  public List<cardType> cardTypes {get;set;}

}

class cardType
{
  public int name {get;set;}
  public DateTime Expiry {get;set;}

}


sample data (Data is returned in 1 table only)
id  Name    cardTypeName    exp
1   a       Amex             1010
1   a        City             2010

desired output
<Emp>
  <ID>1</id>
  <name>1</name>
  <cardTypes>
    <cardType>
            <Name> Amex </Name>
            <exp>  1010  </exp>
    </cardType>
    <cardType>
            <Name> City </Name>
            <exp>  2010  </exp>
    </cardType>
  <cardTypes>
</Emp>

接近1

(dataTable.AsEnumerable()
                     .GroupBy(r => r.ItemArray[1])
                     .Select(grp => new Emp()
                     {
                            id = r.itemarray[1],
                            name = r.itemarray[1],
                        cardTypes = grp.Select(t => new cardType()
                         {
                            field 1,
                            field 2    
                         }).ToList()
                     }));

接近2

 return (from DataRow dr in dataTable.Rows
                        select new Emp
                        {
                            id = "",
                            name= "",
                             cardTypes = dataTable.AsEnumerable()
                                .Select(x => new cardType
                                {
                                   name = ""
                                   exp = ""

                                }).ToList(),
                        });

3 个答案:

答案 0 :(得分:0)

试试这个

return dataTable.AsEnumerable()
            .GroupBy(x => x.Id)
            .Select(x => new
            {
                id = x.Key,
                name = x.Key,
                cardTypes = x.Select(t => new
                {
                    name = t.Name,
                    exp = t.Exp
                })
            });

答案 1 :(得分:0)

考虑到您将Expiry中的cardType字段更改为int

,这应该对您有用
var results =
    dt.AsEnumerable()
        .GroupBy(row => row.ItemArray[0])
        .Select(rows => new Emp
        {
            id = rows.Key,
            name = rows.Key,
            cardTypes = rows.Select(row => new cardType {Name = row.ItemArray[2], Expiry = row.ItemArray[3]})
        });

如果您希望拥有DateTime,请决定如何转化。例如:

cardTypes = rows.Select(row => new cardType {Name = row.ItemArray[2], Expiry = new DateTime(row.ItemArray[3],1,1)})

答案 2 :(得分:0)

如果您要将模型序列化为xml,则应使用[Serializable]属性标记模型,然后使用XmlSerializer

[Serializable]
class Emp
{
  public int id {get;set;}
  public int Name {get;set;}
  public List<cardType> cardTypes {get;set;}

}
[Serializable]
class cardType
{
  public int name {get;set;}
  public DateTime Expiry {get;set;}

}

之后您可以使用以下代码:

static Dictionary<int, Emp> ConvertToDict(DataTable dt)
    {
        Dictionary<int, Emp> emps = new Dictionary<int, Emp>();
        foreach (var dr in dt.AsEnumerable())
        {
            var id = (int)dr["ID"];
            var name = (string)dr["Name"];
            var cardTypeName = (string)dr["CardTypeName"];
            var exp = (int)dr["Exp"];
            Emp emp;
            var cardType = new CardType { Name = cardTypeName, Exp = exp };
            if (emps.TryGetValue(id, out emp))
            {
                emp.CardTypes.Add(cardType);
            }
            else
            {
                emps.Add(id, new Emp { ID = id, Name = name, CardTypes = new List<CardType> { cardType } });
            }
        }
        return emps;
    }

    static List<string> Serialize<T>(IEnumerable<T> entities) where T:new()
    {
        var ser = new XmlSerializer(typeof(T));

        var serializedEntities = entities.Select(entity =>
        {
            using (var sw = new StringWriter())
            {
                ser.Serialize(sw, entity);
                return sw.ToString();
            }
        }).ToList();
        return serializedEntities;
    }