Dynamic linq query is not working with numeric value

c# dynamic-linq winforms

Question

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?

1
0
12/9/2014 1:52:02 PM

Popular Answer

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();
0
4/20/2015 6:52:32 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow