如何在代码中实现ienumerable? [英] How to implement ienumerable in code?
本文介绍了如何在代码中实现ienumerable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨!我想在我的代码中实现不可数,以便它只读,但我不知道在哪里实现它。到目前为止,我还没有找到任何可能解决我问题的互联网解决方案。截至目前,我只使用List并希望在我的代码中集成ienumerable。此外,如果您发现编码错误,请随时提出建议。
我尝试过:
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:
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:
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();
}
推荐答案
使用迭代器方法:
Use an iterator method:
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, ...);
}
}
}
}
或使用 Dapper [ ^ ],正如我建议的那样在您的最新问题 [ ^ ]。
Or use Dapper[^], as I suggested in your latest question[^].
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 });
}
}
这篇关于如何在代码中实现ienumerable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文