C# 计算数字字符串长度的正确方法
我想使用Linq查询来计算字符串的长度,并只返回长度大于7个字符的字符串。以下代码适用于“字符串”:C# 计算数字字符串长度的正确方法,c#,linq,C#,Linq,我想使用Linq查询来计算字符串的长度,并只返回长度大于7个字符的字符串。以下代码适用于“字符串”: public IEnumerable LengthOfNames() { 这是._context.ContextOptions.LazyLoadingEnabled=false; var Query=来自此中的c.\u context.CustomerInfoes 其中c.CustomerName.Length>7 orderby c.CustomerName.Length 选择c.Custom
public IEnumerable LengthOfNames()
{
这是._context.ContextOptions.LazyLoadingEnabled=false;
var Query=来自此中的c.\u context.CustomerInfoes
其中c.CustomerName.Length>7
orderby c.CustomerName.Length
选择c.CustomerName;
返回Query.ToList();
}
但是,当我对“整数”使用类似的查询时,我得到一个错误“'int'不包含'Length'的定义,并且没有扩展方法'Length'接受第一个参数等…”
代码如下:
public IEnumerable<int> LengthOfNumbers()
{
this._context.ContextOptions.LazyLoadingEnabled = false;
var Query = from c in this._context.CustomerInfoes
where c.ContactNo.Length > 7
orderby c.ContactNo.Length
select c.ContactNo;
return Query.ToList();
}
public IEnumerable lengthofNumber()
{
这是._context.ContextOptions.LazyLoadingEnabled=false;
var Query=来自此中的c.\u context.CustomerInfoes
其中c.触点编号长度>7
订货人c.ContactNo.Length
选择c.ContactNo;
返回Query.ToList();
}
作为替代方案,我尝试了以下方法:
public IEnumerable<int> GreaterThanSeven()
{
this._context.ContextOptions.LazyLoadingEnabled = false;
var Query = from c in this._context.CustomerInfoes
where c.ContactNo > 9999999
orderby c.ContactNo
select c.ContactNo;
return Query.ToList();
}
public IEnumerable大于seven()
{
这是._context.ContextOptions.LazyLoadingEnabled=false;
var Query=来自此中的c.\u context.CustomerInfoes
其中c.联系人编号>999999
订购人c.联系人编号
选择c.ContactNo;
返回Query.ToList();
}
这很好用我的问题是:这是计算数字字符串长度的正确(或唯一)方法吗?您的查询(即其中c.ContactNo>9999999
)正确且高效,但您也可以运行此查询
public IEnumerable<int> GreaterThanSeven()
{
this._context.ContextOptions.LazyLoadingEnabled = false;
var Query = from c in this._context.CustomerInfoes
where SqlFunctions.StringConvert((double)c.ContactNo).Length > 7
orderby c.ContactNo
select c.ContactNo;
return Query.ToList();
}
public IEnumerable大于seven()
{
这是._context.ContextOptions.LazyLoadingEnabled=false;
var Query=来自此中的c.\u context.CustomerInfoes
其中SqlFunctions.StringConvert((double)c.ContactNo).Length>7
订购人c.联系人编号
选择c.ContactNo;
返回Query.ToList();
}
您能否定义“数字字符串”或至少显示一个示例?类似于“000333”?如果你有整数,那么你不是在计算字符串的长度。。。您正在计算数字中的小数位数。(例如,如果你用十六进制表示同一个数字,你会得到不同的边界。)也许值得更多地思考你为什么要这样做。当长度很重要时,int
可能不是ContactNo
@HenkHolterman的正确类型我正在学习用Linq
编码,所以这个问题更多的是出于好奇。变量ContactNo
包含虚拟数据。但是,是的,谢谢你的建议。@AlexeiLevenkov就这个例子而言,它可以是类似于“1234567”
或“080801234”
`.ToString().Length>7'创建为查询,这不是一个好主意。@ErikPhilips是的,我在我的文章中提到过这一点answer@RezaRahmati我尝试在代码中使用ToString().Length
。它引发此异常“LINQ to Entities无法识别方法'System.String ToString()'方法,并且此方法无法转换为存储表达式。”
@MarlboroMan_wykyu SqlFunctions位于System.Data.Objects.SqlClient命名空间中,用于在LINQ查询中具有一些基本功能@我明白了。我问这个问题是因为它给了我一个错误,告诉我“名称'SqlFunctions'在当前上下文中不存在”
public IEnumerable<int> GreaterThanSeven()
{
this._context.ContextOptions.LazyLoadingEnabled = false;
var Query = from c in this._context.CustomerInfoes
where SqlFunctions.StringConvert((double)c.ContactNo).Length > 7
orderby c.ContactNo
select c.ContactNo;
return Query.ToList();
}