【发布时间】:2012-01-11 09:10:31
【问题描述】:
下面是我正在使用的代码,但它会回复
方法 'Boolean isUser(System.String)' 不支持对 SQL 的转换。
有什么帮助吗?顺便说一句,我正在使用 linq to SQL 数据源
public void dataBind()
{
using (var gp = new GreatPlainsDataContext())
{
var emp = from x in gp.Employees
let k = isUser(x.ID)
where x.ActivtyStatus == 0
&& isUser(x.ID) != false
orderby x.ID
select new
{
ID = x.ID,
Name = x.FirstName + " " + x.MiddleName
};
ListView1.DataSource = emp;
ListView1.DataBind();
}
}
public static bool isUser(string ID)
{
int temp;
bool x = int.TryParse(ID, out temp);
return x;
}
我找到了一个解决方案,可以将第一个查询的结果作为对象进行查询,但这很好,因为我将通过我的数据两次。
按照安德斯·阿贝尔的建议,在使用类似之后最终工作的更新代码
public void dataBind()
{
using (var gp = new GreatPlainsDataContext())
{
var emp = from x in gp.Employees
where x.ActivtyStatus == 0
&& SqlMethods.Like(x.ID, "[0-9]%")
orderby x.ID
select new
{
ID = x.ID,
Name = x.FirstName + " " + x.MiddleName
};
ListView1.DataSource = emp;
ListView1.DataBind();
}
}
【问题讨论】:
-
Employees.ID 的可能值是什么,为什么可解析为 int 的字符串意味着员工是用户?
-
@spender 关于用户将拥有像“11064”这样的 id 的部分将拥有“oss”。关于我来的原因以及系统是在大平原上建立的:(
-
遗留系统诅咒一旦获得收益。如果检查字段中是否只有数字就足够了,您可以使用 LIKE 表达式。我已经用信息和链接更新了我的答案。
标签: c# .net linq linq-to-sql