0

I am struggling to find a workaround for this issue. LINQ to Entities does not allow to use ToString() method

I am trying to use the following Query

return (from buy in context.ContractBuys
        where (buy.DealerNo.Value.ToString().StartsWith(dlrNo)) &&
        (buy.CreationDate >= createDateFrom) && (buy.CreationDate <= createDateTo)
        select buy).ToList();

dlrNo is a string type. but buy.DealerNo is a Nullable int in C#. Can I use the Like operator?

Khawar Yunus
  • 141
  • 2
  • 15
  • possible duplicate of [LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression](http://stackoverflow.com/questions/5899683/linq-to-entities-does-not-recognize-the-method-system-string-tostring-method) – Magnus Jun 30 '11 at 15:51

2 Answers2

0

Since the dealer number apparently consists of several parts (since you're trying to just look at what the number starts with), it should probably be a string rather than an int, since, after all, it is not really a single number (or maybe you could split it into separate columns). However, if all numbers are of the same length, you can perform an integer division by some power of ten. For instance, if all numbers are seven digits long and you want to look at the first three, divide by 10000. dlrNo should probably be parsed as an int before the query.

int dlrNoAsInt = int.Parse(dlrNo);
return (from buy in context.ContractBuys
       where (buy.DealerNo.Value / 10000 == dlrNoAsInt) && ...;
Aasmund Eldhuset
  • 37,289
  • 4
  • 68
  • 81
0

possible workaround:

var l = (from buy in context.ContractBuys
        where
        (buy.CreationDate >= createDateFrom) && (buy.CreationDate <= createDateTo)
        select buy).ToList();

return l.where(buy => buy.DealerNo.Value.ToString().StartsWith(dlrNo)).toList();
Erix
  • 7,059
  • 2
  • 35
  • 61