我正在我的MVC5应用程序中实现“深度搜索”功能。它查询许多EF实体的关键字(字符串)匹配。
它很棒(感谢Linq!),除了我现在需要执行一些高级字符串操作。
问题是我的“item.Description”字段有时会相当大。我只需要从包含匹配关键字的描述中抓取第一句话。 我需要将其结果限制为250个字符,因此UI看起来并不疯狂。我的控制器返回一个IQueryable对象,它只是一个包含4个字符串属性的类。
以下是项目描述的一些示例:
"Represent clients in criminal and civil litigation and other legal proceedings, draw up legal documents, or manage or advise clients on legal transactions. May specialize in a single area or may practice broadly in many areas of law."
"Teach courses in criminal justice, corrections, and law enforcement administration. Includes both teachers primarily engaged in teaching and those who do a combination of teaching and research."
"Teach courses in law. Includes both teachers primarily engaged in teaching and those who do a combination of teaching and research."
这是我用来过滤结果集的LINQ代码:
var keyword = "law";
var itemsByTitle = db.Items.Where(item => item.Description.Contains(keyword)).Select(item => new GlobalSearchResult { Id = item.Id, Match = item.Description, Title = item.Title, Type = "item" });
我需要扩展item.Description
来执行此高级字符串/句子操作。
理想的结果将是/应该是:
"May specialize in a single area or may practice broadly in many areas of law."
"Teach courses in criminal justice, corrections, and law enforcement administration."
"Teach courses in law."
我看到了这个例子,但它是Linq to Objects,我相信我正在使用Linq to Entities?虽然相关,但语法略有不同。 https://msdn.microsoft.com/en-us/library/mt693048.aspx
在单个LINQ实体查询中是否可以进行高级字符串/句子操作?如果是这样,我该如何开始?我不确定我是否应该在这里使用正则表达式?或者第二个要交叉的对象?