问题
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...
回答1:
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.
回答2:
Have you tried using Substring
instead of the character indexer?
string[] nums = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" };
var b = repository.GetAll().Where(q => nums.Contains(q.BrukerIdent.Substring(0, 1))).ToList()
Note I had to make nums
a string[] instead of a char[] since Substring() returns a string not a char.
Incase that still has a problem running, sometimes SubSonic likes for the object that .Contains()
is being called on to be an IEnumerable instead, so you might have to do:
var nums = (new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" }).AsEnumerable();
回答3:
Did you try to make it without using lambda functions and other stuff in order to see where the issue is ?
回答4:
It needs to convert the linq expression to a valid sql expression.
It seems to me, the method getChars (the indexer on q.BrukerIdent
) is not supported in the IQueryableProvider you use.
来源:https://stackoverflow.com/questions/5829655/subsonic-query-to-determine-if-value-starts-with-numeric