Reputation: 4364
I am trying to check some percent of value in where clause in Linq
following is my code
List<string> v = await context.Categories.Where(w =>(w.id / 2) * 100 == 50)
.Select(w => w.id.ToString()).ToListAsync();
the problem is there is record that meets the condition but no record is selecting. I tried using Convert.ToDecimal(w.id/2)
but its not working either.
How can I get the percent checking in where clause? and why the above code not working?
Upvotes: 1
Views: 173
Reputation: 6864
How about changing the order of operations....
List<string> v = await context.Categories.Where(w => 100 * w.id / 2 == 50)
.Select(w => w.id.ToString()).ToListAsync();
or
List<string> v = await context.Categories.Where(w => 50 * w.id == 50)
.Select(w => w.id.ToString()).ToListAsync();
or
List<string> v = await context.Categories.Where(w => w.id == 1)
.Select(w => w.id.ToString()).ToListAsync();
Upvotes: 3
Reputation: 2804
If id is an integer it does supportswhole numbers so 1/2 = 0. You need to force a cast to a double (a type that supports decimal places) before you operate on it.
List<string> v = await context.Categories.Where(w =>(w.id / 2.0) * 100 == 50)
.Select(w => w.id.ToString()).ToListAsync();
Upvotes: 2
Reputation: 23300
Unless I'm missing something, you don't seem to need the calculation:
w =>(w.id / 2) * 100 == 50
can be written as
w => w.id == 1
Upvotes: 2