I need to use a function in Linq that equivalent to IsNumeric function in SQL. What I am trying to do is get codes that matches a common pattern. The codes should be unique and they are generated by code. The pattern is like this:

ABCD0000001 ABCD0000002 ...

But then user can manuplate the code and enter any character in anywhere in generated code (of course then I check the uniqueness of the code afterwards, but that is out of interest). So the records in database can be

ABCD0000001 ABCD00000T01 ABCDT0000002 ..and so on.

What I want is getting the last programmatically generated (not manipulated by user) codes and retrieve the last numeric digits, so that I can add it by 1 resulting in unique code. (So the last autogenerated code is ABCD0007865 then the new code should be ABCD0007866)

I tried

var lastCode = (
    from c in sedc.Codes
    where SqlMethods.Like(c.Code, "ABCD" + "%")
    where Int64.TryParse(c.Code.Substring(3,10),out t)
    orderby c.Code descending
    select c)
    .FirstOrDefault();

but this code throws an exception saying NotSupportedException: Method 'Boolean TryParse(System.String, Int32 ByRef)' has no supported translation to SQL."

What should I do? Is my way too complicated? Is there a smarter way? Thanks in advance.

有帮助吗?

解决方案

This should work:

var lastCode = (
    from c in sedc.Codes
    where SqlMethods.Like(c.Code, "ABCD[0-9][0-9][0-9][0-9][0-9][0-9][0-9]")
    orderby c.Code descending
    select c)
    .FirstOrDefault();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top