Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert a var type to int?

Tags:

c#

var

asp.net

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.

like image 697
Gurunadh Avatar asked Nov 22 '25 11:11

Gurunadh


1 Answers

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().

like image 181
CodeCaster Avatar answered Nov 25 '25 04:11

CodeCaster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!