Dynamic linq query is not working with numeric value

c# dynamic-linq winforms

Question

Here's 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 as intended when searchValue is a string (for example, ABC), but it throws the following error when it is a numeric number (for example, 30): String and Decimal are incompatible operand types for the operator "=." How can I solve this issue?

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

Popular Answer

You use a parametrized query and send a string value as a parameter; as a result, you get an operand type error.

just provide a decimal number to solve, like in

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