A table called "Player" is read as an object, which has 'BattingAverage' column. I would like to allow the user to search a specific player by a range of min average and max average that user input. I wrote as Lamda (I also tried to add AsEnumerable() after 'player.BattingAverage')
dbcontext.Players.Where(player => player.BattingAverage >= Convert.ToDecimal(txtMin.Text) ||
player.BattingAverage <= Convert.ToDecimal(txtMax.Text))
.OrderBy(player => player.PlayerID).Load();
and normal query
decimal test;
test = Convert.ToDecimal(txtMin.Text) + 4.5m;
txtMax.Text = test.ToString();
var query = from player in dbcontext.Players
where player.BattingAverage >= Convert.ToDecimal(txtMin.Text) ||
player.BattingAverage <= Convert.ToDecimal(txtMax.Text)
select player;
playerBindingSource.DataSource = query.ToList();
playerBindingSource.MoveFirst();
but both I got an error that it says:
LINQ to Entities does not recognize the method 'System.DecimalToDecimal(System.Object)' method and this method cannot be translated into a store expression.
The original table's BattingAverage column value is decimal(3,3)
I also tried
decimal test;
test = Convert.ToDecimal(txtMin.Text) + 4.5m;
txtMax.Text = test.ToString();
and it worked, so I assume that the problem is in the LINQ.
Do the converts outside of your query:
var min=Convert.ToDecimal(txtMin.Text);
var max =Convert.ToDecimal(txtMax.Text);
var query = from player in dbcontext.Players
where player.BattingAverage >= min && player.BattingAverage <= max
select player;
Your Linq to Entities query is going to be translated to sql, but the linq provider is not capable to translate those method calls to a corresponding store function. You can find the list of supported methods in this link