Is it feasible to use string concatenation instead of IDs in a Linq query?

c# dynamic-linq

Question

I have a query which works fine

var queryItems = rawQuery.ObsDataResultList.AsQueryable().Where("Name = @0 AND AuthoredDate = @1" +, selectedItem, dateKey).ToList();

I want to update the query to

var queryItems = rawQuery.ObsDataResultList.AsQueryable().Where("Name = @0 AND AuthoredDate = " + dateKey.ToString(), selectedItem).ToList();

Note the only thing I changed was removing the identifier. Now, I receive Error

Operator '=' incompatible with operand types 'DateTime?' and 'Int32'

How can I fix the error without reverting back to the original query?

1
0
3/23/2012 9:19:42 PM

Accepted Answer

You are missing quotes:

.Where("Name = @0 AND AuthoredDate = \"" + dateKey + "\"")

It seems like a really bad idea to spoil your code like this though. Your original query seems much better.

3
3/23/2012 9:34:04 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