如何将字符串类型转换为int列表

时间:2018-12-18 06:07:31

标签: c#

我正在使用应用程序并使用.dll文件。字段之一的数据类型是字符串,我想将其存储在列表中。当我尝试存储它时,会给我

  

无法将类型'string'隐式转换为'System.Collections.Generic.List”

This is an error which is occuring

if (grid.SortedColumn?.Index == e.ColumnIndex)
{
    var sortIcon = grid.SortOrder == SortOrder.Ascending ?
        VisualStyleElement.Header.SortArrow.SortedUp : 
        VisualStyleElement.Header.SortArrow.SortedDown;
    var renderer = new VisualStyleRenderer(sortIcon);
    var size = renderer.GetPartSize(e.Graphics, ThemeSizeType.Draw);
    renderer.DrawBackground(e.Graphics,
        new Rectangle(e.CellBounds.Right - size.Width,
        e.CellBounds.Top, size.Width, e.CellBounds.Height));
}

这实际上是我的代码Obj.USIn是我正在使用自己的类型为dynamic的变量的.Dll文件 obj.InvoiceType = 1; fbrlist = obj.USIN;//(1:New, 2:Debit, 3:Credit) List<int> fb = new List<int>(); fb = fbrlist; obj.Items = Items(); abc = obj; 中使用的代码。请让我知道我该怎么办。帮助将不胜感激。预先感谢

fbrlist

这里obj.Items = Items();是`私有List Items()//参数中的项目列表

foreach (DataRow dr in ds1.Tables[0].Rows)
        {               
        // start loop for multiple invoices
        Invoice obj = new Invoice();
        int POSID = 110033;         

        obj.FBRInvoiceNumber = string.Empty;
        obj.POSID = POSID;
        obj.USIN = dr["INV_NUM"] != DBNull.Value ? Convert.ToString(dr["INV_NUM"]) : Convert.ToString("");
        obj.DateTime = System.DateTime.Now;// "2018-11-01 00:00:00.000";
        obj.BuyerName = dr["BUY_NOM"] != DBNull.Value ? Convert.ToString(dr["BUY_NOM"]) : Convert.ToString("");
        obj.BuyerPhoneNumber = dr["BUY_PHO"] != DBNull.Value ? Convert.ToString(dr["BUY_PHO"]) : Convert.ToString("");
        obj.TotalBillAmount = dr["TOT_BIL"] != DBNull.Value ? Convert.ToDouble(dr["TOT_BIL"]) : Convert.ToDouble(0);
        obj.TotalQuantity = dr["TOT_QTY"] != DBNull.Value ? Convert.ToDouble(dr["TOT_QTY"]) : Convert.ToDouble(0);
        obj.TotalSaleValue = dr["TOT_VAL"] != DBNull.Value ? Convert.ToDouble(dr["TOT_VAL"]) : Convert.ToDouble(0);
        obj.TotalTaxCharged = dr["TOT_TAX"] != DBNull.Value ? Convert.ToDouble(dr["TOT_TAX"]) : Convert.ToDouble(0);
        obj.Discount = dr["TOT_DIS"] != DBNull.Value ? Convert.ToDouble(dr["TOT_DIS"]) : Convert.ToDouble(0);
        obj.PaymentMode = dr["PAY_MOD"] != DBNull.Value ? Convert.ToInt32(dr["PAY_MOD"]) : Convert.ToInt32(0);  // (1: Cash, 2: Card);            
        obj.RefUSIN = string.Empty;
        obj.InvoiceType = 1;

            fbrlist = obj.USIN;//(1:New, 2:Debit, 3:Credit)               
          List<int> fb = new List<int>();
            string inputString = fbrlist;

            string[] stringArray = inputString.Split(',');

            List<int> intList = stringArray.Select(x => Convert.ToInt32(x)).ToList();
            foreach (int i in intList)
            {
                List<int> ab = intList;
            }




            obj.Items = Items();

            abc = obj;

            //ViewBag.FBR_Response = response;          
            fbr = obj.USIN;

        }

3 个答案:

答案 0 :(得分:1)

您需要先用分隔符(,)分割输入字符串,然后将每个分割后的字符串转换为整数,如

string inputString = "1234,1235,1236,1237";

string[] stringArray = inputString.Split(',');

List<int> intList = stringArray.Select(x => Convert.ToInt32(x)).ToList();

foreach (int i in intList)
{
    Console.WriteLine(i);
}

Console.ReadLine();

输出:

enter image description here

答案 1 :(得分:0)

您可以使用以下方法将值添加到int列表中:

List<string> strList = new List<string>();
List<int> intList = new List<int>();

strList.Add("1");
strList.Add("2");
strList.Add("3");

foreach(string el in strList)
{
   intList.Add(Convert.ToInt32(el));
}

答案 2 :(得分:0)

为避免期望值123y出现异常,您可以int.TryParse

List<string> strList = new List<string>();
List<int> intList = new List<int>();

strList.Add("1");
strList.Add("2");
strList.Add("3");
strList.Add("35tdfsfd");
foreach (string el in strList)
{
  int validInt;
  if (int.TryParse(el, out validInt))
  {
     intList.Add(validInt);
  }
}