简体   繁体   English

如何将 IEnumerable 类型转换为 List 类型?

[英]How to convert IEnumerable type to List type?

I am trying to fetch certain database values and store them in a datatable.我正在尝试获取某些数据库值并将它们存储在数据表中。 After that I am applying linq to group by all the data with account number.之后,我应用 linq 对所有带帐号的数据进行分组。 I am storing the final result in a list which is of the following class type -我将最终结果存储在以下 class 类型的列表中 -

  class ListItems
    {
        public string accountNumbers
        {
            get;
            set;
        }
         
        public string itemNumbers
        {
            get;
            set;
        }

    }

My code is:我的代码是:

List<ListItems> accounts = new List<ListItems>();

        public void GetItems(int ID1, int ID2)
        {
            con = new SqlConnection(connection);
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_FetchItems";
            cmd.Parameters.AddWithValue("@id1", ID1);
            cmd.Parameters.AddWithValue("@id2", ID2);
            cmd.Connection = con;
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            adp.Fill(dt);

            accounts = from result in dt.AsEnumerable() 
                       group result by result.Field<string>("accountNumber");
           
        }

I am getting the following error我收到以下错误

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.Linq.IGrouping<string, System.Data.DataRow>>' to 'System.Collections.Generic.List<PricingPractice.ListItems>无法将类型“System.Collections.Generic.IEnumerable<System.Linq.IGrouping<string, System.Data.DataRow>>”隐式转换为“System.Collections.Generic.List<PricingPractice.ListItems>”

on line在线的

 accounts = from result in dt.AsEnumerable() 
                       group result by result.Field<string>("accountNumber");

I even tried to convert this expression using ToList() but it is also not working.我什至尝试使用 ToList() 转换此表达式,但它也不起作用。

Try this:试试这个:

class ListItem
{
    public string accountNumber { get; set; }
     
    public List<string> itemNumbers { get; set; }
}

accounts = (from result in dataTable.AsEnumerable() group result by result.Field<string>("accountNumber") into g select new ListItems { accountNumber = g.Key, itemNumbers = g.Select(x => x.Field<string>("<itemNumber>")).ToList() }).ToList();

An explanation is in order:解释如下:

  1. I changed your ListItems class to ListItem and the itemNumbers property to be of type List<string>我将您的ListItems class 更改为ListItem并将itemNumbers属性更改为List<string>类型
  2. I am grouping all items (, I don't know the name of the column in your database) to a single ListItem instance, grouped by the accountNumber column, and storing all items in the itemNumbers property我将所有项目(我不知道您数据库中列的名称)分组到单个ListItem实例,按accountNumber列分组,并将所有项目存储在itemNumbers属性中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM