c# - linq中的子串

标签 c# linq

我的数据如下,我想对其运行 linq 查询

MyTable
ID        DATA
1         1:40:567

我的问题是

var query = (from tab in MyTable
             where tab.DATA.Substring(0, tab.DATA.IndexOf(":")) == "1"))
             select new
             {
                tab.ID,
                tab.DATA
             }).ToList();

我得到一个错误

InnerException {"Invalid length parameter passed to the LEFT or SUBSTRING function."} System.Exception {System.Data.SqlClient.SqlException}

我想要数据列的子字符串。请帮忙

最佳答案

由于您只有一个char 需要验证,请尝试使用substring 来获取一个字符,例如:

var data= (from tab in MyTable
             where tab.DATA.Substring(0, 1) == "1")
             select new
             {
                tab.ID,
                tab.DATA
             }).ToList();

关于c# - linq中的子串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32119761/

相关文章:

c# - 使用 LINQ 连接字符串

C# SerialPort.ReadExisting 错误

c# - 如何在 C# 中使用 Webclient 填写表单并提交

需要 C# 正则表达式

c# - 使用 Linq 获取 parent 曾孙的 "lowest"日期

c# - 如何检查一个项目是否存在于字符串列表中,如果存在,则获取其索引

c# - 如何从 Json.NET 获取 key 列表?

c# - 在 C# 中从 Go 调用函数

linq - IEnumerable 和 IQueryable 混淆

c# - 跨多个字符串的不同字符