将数据表转换为包含数组的用户定义类

本文关键字:用户 定义 数组 包含 数据表 转换 | 更新日期: 2023-09-27 17:51:12

我有这个对象结构

public class Product
{
public int Id {get;set;}
public string DisplayName{get;set;}
public Ingredient[] Ingredents{get;set;}
}
public class Ingredient
{
 public int id{get;set;}
 public string Name{get;set;}
}

我有一个结果集

的数据表
    Id | DisplayName  |  IngredientId | IngName
-------------+-------------------------+------------------------
    1  | Prod1         |   1           |  Ing1
    1  | Prod1         |   2           |  Ing2
    1  | Prod1         |   3           |  Ing3
    2  | Prod2         |   1           |  Ing1
    2  | Prod2         |   2           |  Ing2
    3  | Prod3         |   1           |  Ing1
    3  | Prod3         |   2           |  Ing2

我需要有LINQ查询,这将给我的产品列表如下://描述结构的伪代码:

resultProductList =
{
  new Product() { Id = 1
                , DisplayName = "Prod1"
                , Ingredients = {
                                 new Ingredient{ Id = 1, Name = "Ing1" } 
                                , new Ingredient{ Id = 2, Name = "Ing2" } 
                                , new Ingredient{ Id = 3, Name = "Ing3" } 
                                }
 , new Product() { Id = 2
                , DisplayName = "Prod2"
                , Ingredients = {
                                 new Ingredient{ Id = 1, Name = "Ing1" } 
                                , new Ingredient{ Id = 2, Name = "Ing2" } 
                                }
 , new Product() { Id = 3
                , DisplayName = "Prod3"
                , Ingredients = {
                                 new Ingredient{ Id = 1, Name = "Ing1" } 
                                , new Ingredient{ Id = 2, Name = "Ing2" } 
                                }
}

我参考了这个,但是我无法得到解决方案。

List<Product> lstContracttype = dsGetProducts.Tables[0]
                                      .AsEnumerable()
                                      .Select(x => new Product
                                      {
                                          Id= Convert.ToInt32(x[0]),
                                          DisplayName= Convert.ToString(x[1])
                                      })
                                      .GroupBy(y => y.id).ToList();

谁能建议我怎么写LINQ查询得到这个结果?

更新:我又添加了一个子类,并将其添加到主产品类中。见下文:

public class Product
    {
    public int Id {get;set;}
    public string DisplayName{get;set;}
    public Ingredient[] Ingredents{get;set;}
    public Price MyPrice{get;set;}
    }
public class Price
{
    public int min {get;set;}
    public int max {get;set;}
    public int defaultPrice {get;set;}
}

我修改了LINQ查询如下,并得到了结果。我的问题是在下面的LINQ查询,我应该写.First<Price>还是.FirstOrDefault<Price> ?这是处理这种情况的正确方法还是有其他方法?

var lstProducts = dsGetProducts.Tables[0].AsEnumerable().Cast<DataRow>()
                  .GroupBy(r => new { Id = r["Id"], 
                                      DisplayName = r["DisplayName"], 
                                      priceMax = r["priceMax"],
                                      priceMin = r["priceMin"],
                                      priceDefault = r["priceDefault"]
                                    })
                  .Select(g => new Product()
                  {
                     Id = (int)g.Key.Id,
                     DisplayName = (string)g.Key.DisplayName,
                     Ingredients = g.Select(r => new Ingredient()
                     {
                        id = (int)r["IngredientId"],
                        Name = (string)r["IngName"]
                     }).ToArray(),
                     MyPrice =  g.Select(r => new Price()
                     {
                        min= (int)r["minPrice"],
                        max= (int)r["maxPrice"],
                        defaultPrice= (int)r["priceDefault"],
                     }).FirstOrDefault<Price>()

将数据表转换为包含数组的用户定义类

你需要然后项目:

dataTable.Rows.Cast<DataRow>()
    .GroupBy(r => new { Id = r["Id"], DisplayName = r["DisplayName"] })
    .Select(g => new Product()
    {
        Id = (int)g.Key.Id,
        DisplayName = (string)g.Key.DisplayName,
        Ingredients = g.Select(r => new Ingredient()
        {
            id = (int)r["IngredientId"],
            Name = (string)r["IngName"]
        }).ToArray()
    });

你需要Cast<DataRow>DataTable从。net 1.1开始就出现了,那时泛型还不存在。因此,枚举器产生object个实例。

考虑到注释,你可以使用另一个选项,前提是你已经安装了。net Framework 3.5,使用System.Data.DataSetExtensions中的Field<T>扩展方法。请确保向程序集添加引用:

dataTable.Rows.Cast<DataRow>()
    .GroupBy(r => new { Id = r.Field<int>("Id"), DisplayName = r.Field<string>("DisplayName") })
    .Select(g => new Product()
    {
        Id = g.Key.Id,
        DisplayName = g.Key.DisplayName,
        Ingredients = g.Select(r => new Ingredient()
        {
            id = r.Field<int>("IngredientId"),
            Name = r.Field<string>("IngName")
        }).ToArray()
    });