Click here to Skip to main content
15,895,192 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi! i wanted to implement ienumerable in my code so that it is readonly but i don't know where to possibly implement it. So far i haven't found any solution in the internet that might answer my problem. As of now, i have only used List and wanted to integrate ienumerable in my code. Also, feel free to suggest anything if you notice bad coding practices.

What I have tried:

C#
public static List<GuitarItems> GetGuitarItems(string itemCategory)
    {
        List<GuitarItems> list = new List<GuitarItems>();
        string query = string.Format("SELECT * FROM guitarItems WHERE brand LIKE @brand");

        try
        {
            conn1.Open();
            command1.CommandText = query;
            command1.Parameters.Add(new SqlParameter("brand", itemCategory));
            SqlDataReader reader = command1.ExecuteReader();

            while (reader.Read())
            {
                int id = reader.GetInt32(0);
                string type = reader.GetString(1);
                string brand = reader.GetString(2);
                string model = reader.GetString(3);
                double price = reader.GetDouble(4);
                string itemimage1 = reader.GetString(5);
                string itemimage2 = reader.GetString(6);
                string description = reader.GetString(7);
                string necktype = reader.GetString(8);
                string body = reader.GetString(9);
                string fretboard = reader.GetString(10);
                string fret = reader.GetString(11);
                string bridge = reader.GetString(12);
                string neckpickup = reader.GetString(13);
                string bridgepickup = reader.GetString(14);
                string hardwarecolor = reader.GetString(15);

                GuitarItems gItems = new GuitarItems(id, type, brand, model, price, itemimage1, itemimage2, description, necktype, body,
                    fretboard, fret, bridge, neckpickup, bridgepickup, hardwarecolor);
                list.Add(gItems);
            }
        }
        finally
        {
            conn1.Close();
            command1.Parameters.Clear();
        }

        return list;
    }


And here is the other code:
C#
private void FillPage()
    {
        List<GuitarItems> itemList = new List<GuitarItems>();
        List<string> itemListPage = new List<string>();

        itemList = ConnectionClassGuitarItems.GetGuitarItems(brandType);
       
        StringBuilder sb = new StringBuilder();

        foreach (GuitarItems gList in itemList)
        {
            itemListPage.Add("GuitarItemsIbanezDetails" + (x + 1) + ".aspx");
           
            sb.Append(
                    string.Format(
                        @"
                        <div class='one-two'>
                            <a href='{3}' runat='server'><img runat='server' src='{0}'/></a>
                            <div class='content'>
                                <div id='label'>{1} {2}</div>
                            </div>
                        
                    </div>", gList.ItemImage1, gList.Brand, gList.Model, itemListPage[x]));

            x++;
            
        }
  

        lblOutput.Text = sb.ToString();

    }
Posted
Updated 1-Jun-17 2:47am
Comments
[no name] 28-May-17 11:12am    
So use a readonly collection.
BebeSaiyan 31-May-17 2:06am    
@NotPoliticallyCorrect - yea right very helpful
Kornfeld Eliyahu Peter 29-May-17 3:08am    
What is the connection between IEnumerable and read-only collection?

1 solution

Use an iterator method:
C#
public static IEnumerable<GuitarItems> GetGuitarItems(string itemCategory)
{
    using (var con = new SqlConnection("..."))
    using (var cmd = new SqlCommand("SELECT * FROM guitarItems WHERE brand LIKE @brand", con))
    {
        cmd.Parameters.AddWithValue("@brand", itemCategory);
        
        con.Open();
        using (var reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
        {
            while (reader.Read())
            {
                int id = reader.GetInt32(0);
                ...
                
                yield return new GuitarItems(id, ...);
            }
        }
    }
}

Or use Dapper[^], as I suggested in your latest question[^].
C#
public static IEnumerable<GuitarItems> GetGuitarItems(string itemCategory)
{
    using (var con = new SqlConnection("..."))
    {
        return con.Query<GuitarItems>("SELECT * FROM guitarItems WHERE brand LIKE @brand", new { brand = itemCategory });
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900