0

This is my code:

// Taken from combobox selection.
string columnName = cboCrudSearchColumn.Text.ToString(); 

// Taken from textbox selection.
string searchValue = txtCrudSearch.Text.ToString(); 

dgvLoadTable.DataSource = EntityName
    .TableName
    .Where(columnName + " = @0", searchValue )
    .ToList();

Now, this works fine when searchValue is a string (ex: ABC), but when it is a numeric value (ex: 30) it gives the following exception: Operator '=' incompatible with operand types 'Decimal' and 'String. How can I overcome this problem?

Paradox
  • 129
  • 1
  • 3
  • 14
  • Looks to me like you're trying to include a parameter. Note paratmeters can't start with numeric or symbolic characters (much like variables). – jbutler483 Dec 09 '14 at 13:35
  • With the code in your example searchValue is always a string. – Magnus Dec 09 '14 at 13:47
  • That's what I was thinking, then why does it throw the exception? It also doesn't work with DateTime. – Paradox Dec 09 '14 at 13:51
  • 2
    @Paradox probably since the column it is comparing with is not a string. Example `Amount = 'Hej'` where Amount is of type decimal would not work very well. – Magnus Dec 09 '14 at 13:57

1 Answers1

0

You use parametrized query and pass as parameter string value, so this parameter have type string and you get error with operand types.

for solving just pass decimal value, like

dgvLoadTable.DataSource = EntityName
    .TableName
    .Where(columnName + " = @0", int.Parse(searchValue) )
    .ToList();
Grundy
  • 13,356
  • 3
  • 35
  • 55