亚音速查询以确定值是否以数字开头

发布于 2024-11-04 01:46:19 字数 592 浏览 1 评论 0原文

这是 的后续内容这个问题,但是上下文已经改变了。打破了公认的解决方案。

这次我尝试使用 SubSonic,但它使用之前接受的解决方案抛出错误消息

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: 

据我所知 q.BrukerIdent 是一个字符串。所以我有点迷茫了...

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

诗化ㄋ丶相逢 2024-11-11 01:46:19

忘记 LINQ - 不要使用它。有更好的方法来实现您的目标。使用 SQL。如果您只使用 SQL,那么您已经完成了 - 它只是一个 基本模式搜索

亚音速?使用 CodingHorror 并使用适当的 SQL。

CodingHorror 是 SubSonic 程序集中的一个类。它为您提供了一种执行特定的、手写 SQL 的方法,在这些情况下,尝试使用 LINQ 获得相同的结果即使不是不可能,也是很困难的,因此完全是浪费时间

您还可以执行 CodingHorror 查询并要求它以强类型对象列表的形式提供结果。

这是 CodingHorror 的用法,应该可以解决您的问题,但您必须填写一些详细信息(表名称、类型名称)。

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

此外,人们已经开始放弃使用 SubSonic,甚至更大的 ORM。我建议查看 PetaPoco (或 Massive 或 Dapper 等)。 PetaPoco 的作者受到在使用 SubSonic 的项目中过于频繁地使用 CodingHorror 的经历的驱使。

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.

救星 2024-11-11 01:46:19

您是否尝试过使用 Substring 而不是字符索引器?

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()

请注意,我必须将 nums 设为 string[] 而不是 char[],因为 Substring() 返回字符串而不是 char。

如果运行仍然存在问题,有时 SubSonic 喜欢将调用 .Contains() 的对象改为 IEnumerable,因此您可能必须这样做:

var nums = (new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" }).AsEnumerable();

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();
蓝眼泪 2024-11-11 01:46:19

您是否尝试在不使用 lambda 函数和其他东西的情况下完成它,以查看问题出在哪里?

Did you try to make it without using lambda functions and other stuff in order to see where the issue is ?

思念绕指尖 2024-11-11 01:46:19

它需要将 linq 表达式转换为有效的 sql 表达式。

在我看来,您使用的 IQueryableProvider 不支持 getChars 方法(q.BrukerIdent 上的索引器)。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文