Search code examples
c#.netlinqsubsonic

Subsonic query to determine if value starts with numeric


This is a follow-up of this question, however the context has changed. Breaking the accepted solution.

This time I'm trying to use SubSonic, but it throws an errormessage using the previous accepted solution

System.NotSupportedException: The method 'get_Chars' is not supported
...
Line 36:             char[] nums = "0123456789".ToCharArray();
Line 37: 
Line 38:             var b = repository.GetAll().Where(q => nums.Contains(q.BrukerIdent[0])).ToList();
Line 39: 
Line 40: 

As far as I can tell q.BrukerIdent is a string. So I'm a bit thrown...


Solution

  • Forget LINQ - Don't use it. There is a better way to accomplish your goal. Use SQL. You'd be done already if you would've just used SQL - it's just a basic pattern search.

    Subsonic? Use a CodingHorror and use the appropriate SQL.

    CodingHorror is a class that is in the SubSonic assembly. It gives you a way to execute specific, hand-written SQL for instances where trying to achieve the same result with LINQ would be difficult if not impossible, and therefore a complete waste of time.

    You can also execute a CodingHorror query and ask it to give you the results as a list of your strongly-typed objects.

    Here's a use of CodingHorror that should solve your problem, but you'll have to fill in a couple of the particulars (table name, type name).

    var query = new CodingHorror(@"SELECT * FROM YourTableHere 
                                   WHERE BrukerIdent LIKE '[a-z0-9]%'");
    var matchingItems = query.ExecuteTypedList<YourTypeHere>();
    

    Also, there's been a bit of an exodus from using SubSonic, and even larger ORM's in general. I recommend looking at PetaPoco (or Massive or Dapper, etc.). The author of PetaPoco was driven by his experience of having to use CodingHorror far too often in projects that utilized SubSonic.