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. I am storing the final result in a list which is of the following class type -
My code is :
I am getting the following error on line no 16
I even tried to convert this expression using ToList() but it is also not working.
C#:
class ListItems
{
public string accountNumbers
{
get;
set;
}
public string itemNumbers
{
get;
set;
}
}
My code is :
C#:
List<ListItems> accounts = new List<ListItems>();
public void GetItems(int siteID1, int siteID2)
{
con = new SqlConnection(connection);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sp_FetchItems";
cmd.Parameters.AddWithValue("@siteid1", siteID1);
cmd.Parameters.AddWithValue("@siteid2", siteID2);
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 on line no 16
C#:
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.Linq.IGrouping<string, System.Data.DataRow>>' to 'System.Collections.Generic.List<PricingPractice.ListItems>
I even tried to convert this expression using ToList() but it is also not working.
Last edited: