简体   繁体   中英

Linq - Specified cast is not valid

IEnumerable<DataRow> drIEOrderDetail = 
   from data in dsOrder.Tables["ORDDETL02"].AsEnumerable()
   where data.Field<int>("INOWNER") == int.Parse(txtInventoryOwner.Text.Trim()).ToString()
   && data.Field<string>("SKUCODE") == txtSKUCode.Text.Trim()
   &&  data.Field<string>("PACKID") == cmbPackageId.SelectedValue.ToString()
   &&  data.Field<string>("BATCH") == txtBatch.Text.ToString()                                                           
   select data;

I'm getting error in:

data.Field<int>("INOWNER") == int.Parse(txtInventoryOwner.Text.Trim()).ToString()

where INOWNER datatype is Integer. Integer and String both is not accepting.

You specified the type of INOWNER is int, but you tried to convert txtInventoryOwner.Text to text object. Try removing the ToString()

data.Field<int>("INOWNER") == int.Parse(txtInventoryOwner.Text.Trim());

删除您的 ToString():

   where data.Field<int>("INOWNER") == int.Parse(txtInventoryOwner.Text.Trim())

You mention that data.Field("INOWNER") is of type int.

Would you expect the following to compile?

int myInt = data.Field<int>("INOWNER");
string myString = int.Parse(txtInventoryOwner.Text.Trim()).ToString();
bool b = myInt == myString;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM