使用 LINQ 分割字符串

发布于 2024-12-23 06:30:05 字数 572 浏览 3 评论 0原文

我想按我的结果和字符串行中的匹配数进行排序。

这是代码

.ThenByDescending(p => p.Title.ToLower()
                              .Split(' ')
                              .Count(w => words.Any(w.Contains)));

,但它给我带来了错误,并表示 LINQ 无法将 Split 解析为 SQL。

LINQ to Entities 无法识别方法“System.String[]” Split(Char[])' 方法,并且该方法不能翻译为 存储表达式。

如何通过 LINQ 实现 Split?

例如,对于这个数组,它必须以这种方式排序

words = { "a", "ab" }

ab a ggaaag gh //3 matches
ba ab ggt //2 matches
dd //0 matches

I want to order by my results with the matches count in my string line.

So here is code

.ThenByDescending(p => p.Title.ToLower()
                              .Split(' ')
                              .Count(w => words.Any(w.Contains)));

But it bring me error and says that LINQ can't parse Split into SQL.

LINQ to Entities does not recognize the method 'System.String[]
Split(Char[])' method, and this method cannot be translated into a
store expression.

How can I implement Split via LINQ?

For example, for this array it must order in this way

words = { "a", "ab" }

ab a ggaaag gh //3 matches
ba ab ggt //2 matches
dd //0 matches

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

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

发布评论

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

评论(3

醉殇 2024-12-30 06:30:05

这意味着 Linq toEntity 无法找到可以编写为 sql 查询的 split 方法的翻译。如果你想执行分割函数,你必须通过调用ToList()、AsEnumerable()等将记录放入内存。

var result = (from t in db.Table
              select t).AsEnumerable().OrderBy(x=>x.Column).ThenByDescending(p=>p.Title.ToLower.Split(' ')....);

it means that Linq to entities failed to find translation of split method that can be written as sql query. if you want to perform split functions you have to bring the record in memory by calling ToList(), AsEnumerable() etc.

var result = (from t in db.Table
              select t).AsEnumerable().OrderBy(x=>x.Column).ThenByDescending(p=>p.Title.ToLower.Split(' ')....);
乙白 2024-12-30 06:30:05

您需要在 LINQ to Objects 中执行排序,因为 LINQ to Entities 无法将 C# 代码转换为 SQL(或您正在使用的任何数据库的语言)。

你可以这样做。

var results = 
  objectContext
  .Where(a => a == b) //Whatever
  .AsEnumerable()
  .ThenByDescending(p=>p.Title.ToLower().Split(' ').Count(w=>words.Any(w.Contains)));

AsEnumerable()(与 ToArray()ToList() 一起使用)将 LINQ to Entities 转换回 LINQ to Objects。

You will need to perform the sorting in LINQ to Objects because LINQ to Entities cannot translate the C# code into SQL (or the language of whatever DB you're using).

You can do it like this.

var results = 
  objectContext
  .Where(a => a == b) //Whatever
  .AsEnumerable()
  .ThenByDescending(p=>p.Title.ToLower().Split(' ').Count(w=>words.Any(w.Contains)));

AsEnumerable() (along with ToArray() or ToList()) turn the LINQ to Entities back into LINQ to Objects.

慕巷 2024-12-30 06:30:05

人们不能指望 LINQ to Entities 能够将其转换为 SQL。

最好的解决方案是更改架构,以便将帖子标题中的每个单词作为单独的行存储在单独的表中(具有适当的关联)。然后,查询可以使用显式(组)连接操作或 FK 关联属性。

如果您无法做到这一点,但又希望查询在数据库上运行,则必须考虑编写用户定义的函数来处理分隔字符串。阅读此问题了解更多信息。但这将是一个很大的工作量。

最简单的解决方案(如果您有能力这样做)当然是将所有内容拉回到客户端,只需使用 LINQ to Objects 来处理该部分的查询,如 @Muhammad Adeel Zahid 所提到的。

One can't expect LINQ to Entities to be able to convert that to SQL.

The best solution is to change the schema such that each word in a post's title is stored as a separate row in a separate table (with the appropriate associations). The query can then use explicit (group) join operations or the FK association property.

If you can't do this and yet want the query to run on the database, you'll have to look into writing a user-defined function to work with delimited strings. Read this question for more info. This will be a lot of work though.

The easiest solution (if you can afford to do it) of course is to pull everything back to the client just use LINQ to Objects for that part of the query, as mentioned by @Muhammad Adeel Zahid.

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