Search code examples
c#asp.netvar

How can I convert a var type to int?


Here from the following, I am getting a var type variable in my code:

var FormID = from n in dtEnDate.AsEnumerable()
             where n.Field<int>("Tax_Setup_UID") ==
                   Convert.ToInt32(item.GetDataKeyValue("Tax_Setup_UID"))
             select n.Field<string>("FormID");

Here it is giving me the formid. Now I want to convert this var type variable to an int for next use and if I try without conversion it is giving me an error. But I can't do it.


Solution

  • You're returning select n.Field<string>("FormID"), so the actual type of var FormID will be IEnumerable<string>.

    If you know the query will return one result, you can do something like:

    int formID = int.Parse(FormID.First());
    

    If FormID is an int in your database, change n.Field<string> to n.Field<int> and you can omit the int.Parse().