Sql 用于数字匹配的LINQ查询
我有一个名为dbo的表。Question有两列Id和Title。我想编写LINQ查询,以获取问题标题以数字开头的所有问题 以下是“标题”以数字开头的问题搜索结果示例:Sql 用于数字匹配的LINQ查询,sql,linq,Sql,Linq,我有一个名为dbo的表。Question有两列Id和Title。我想编写LINQ查询,以获取问题标题以数字开头的所有问题 以下是“标题”以数字开头的问题搜索结果示例: 5个有用的VisualStudio快捷方式 关于Hadoop需要了解的7件事 你应该感谢黑客的10个理由 SQL查询类似于: SELECT Title FROM dbo.Question WHERE Title NOT LIKE '[a-z]%' 上述SQL查询的LINQ等价物是什么 var query = from x i
- 5个有用的VisualStudio快捷方式
- 关于Hadoop需要了解的7件事
- 你应该感谢黑客的10个理由
SELECT Title FROM dbo.Question WHERE Title NOT LIKE '[a-z]%'
上述SQL查询的LINQ等价物是什么
var query = from x in dbcontext.Questions
where SqlFunctions.IsNumeric(EntityFunctions.Left(x.Title, 1)) == 1
select x;
资料来源:
想想看,这不是很有效,因为它不使用索引。你可以这样做
IEnumerable<string> digits = Enumerable.Range(0, 10) // 0-9
.Select(i => i.ToString());
//Create a query for starts with on each digit.
IEnumerable<IQueryable<Question>> questions = digits
.Select(i => dbcontext.Questions.Where(q => q.StartsWith(i))
IQueryable<Question> concatedTogether = .Aggregate(Queryable.Concat) //Union all each of them together
int count = concatedTogether.Count();
您可以在这里找到答案:@PeterRing既不能用于实体框架,也不能用于linqtosql。
int count = Enumerable.Range(0, 9)
.Select(int.ToString)
.Select(i => dbcontext.Questions.Where(q => q.StartsWith(i))
.Aggregate(Queryable.Concat)
.Count();