【发布时间】:2020-12-15 04:59:13
【问题描述】:
大家好 我尝试将一组ٍStrings转换为一个类,然后将这些元素添加到同一类的数组或列表中
问题 一切都很好,只有添加一个元素时,它才会将数组中的所有值更改为与最后一个元素相同的值
TxtCookie.Text:
1=|257|9.5|1|true|true|true|true|1-来自网络,2=|259|11.5|7|false|false|false|false|232-来自网络,3=| 261|9.5|5|true|false|true|true|-来自网络,4=|267|9.5|1|true|true|true|true|-来自网络
此代码:
//Get The Value from Text Box To list of Strings
string[] lst = TxtCookie.Text.Split(',');
//Divide each element into a set of values
var D = (from a in lst select a.Split('|')).ToList();
//Define an object from the class
TblInvoiceContent tblInvoiceContent = new TblInvoiceContent();
//Define an List from the class
List<TblInvoiceContent> TBLIC = new List<TblInvoiceContent>();
//Here I take the values and configure them according to the class structure
foreach (var item in D)
{
tblInvoiceContent.ItremID = Convert.ToInt32(item[1]);
tblInvoiceContent.SilingPrice = Convert.ToDouble(item[2]);
tblInvoiceContent.Quantity = Convert.ToInt32(item[3]);
tblInvoiceContent.mayonnaise = Convert.ToBoolean(item[4]);
tblInvoiceContent.ketchup = Convert.ToBoolean(item[5]);
tblInvoiceContent.Hot = Convert.ToBoolean(item[6]);
tblInvoiceContent.garlic = Convert.ToBoolean(item[7]);
tblInvoiceContent.Reqomindition = item[8].ToString();
//Here I add the item to the list
TBLIC.Add(tblInvoiceContent);
}
//Here I am displaying the list items
GridView1.DataSource = TBLIC;
GridView1.DataBind();
结果:
【问题讨论】:
标签: c# entity-framework for-loop arraylist foreach