Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试在linq语句的where中调用方法_C#_.net_Linq_Linq To Sql - Fatal编程技术网

C# 尝试在linq语句的where中调用方法

C# 尝试在linq语句的where中调用方法,c#,.net,linq,linq-to-sql,C#,.net,Linq,Linq To Sql,下面是我正在使用的代码,但它会以 方法“Boolean isUser(System.String)”不支持到SQL的转换 有什么帮助吗?顺便说一句,我使用的是linqtosql数据源 public void dataBind() { using (var gp = new GreatPlainsDataContext()) { var emp = from x in gp.Employees let k = isUser(x.I

下面是我正在使用的代码,但它会以

方法“Boolean isUser(System.String)”不支持到SQL的转换

有什么帮助吗?顺便说一句,我使用的是linqtosql数据源

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;
}
我找到了一个解决方案,可以将第一次查询的结果作为对象进行查询,但这是一个很好的原因,因为我会将数据传递两次


按照Anders Abel的建议,在使用类似的代码之后,最终有效的更新代码

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();
        }
    }

您遇到的问题是,因为查询需要在数据库上运行,所以只能使用在数据库上运行的东西,而
isUser
方法中的C代码不能在数据库上运行


您必须在不使用该函数的情况下重新编写它。也许您有一个表,按您可以加入的ID列出用户?

Linq to sql将查询转换为sql。它只知道如何翻译有限的一组内置函数。您必须重写查询,使其不包含自己的函数

有关linq to sql支持的函数和运算符的完整列表,请访问


您可以使用
SqlMethods.Like()
检查字段是否只包含数字。请参阅以获取示例。

查询对您有效的
ID将更容易,例如
其中ID>0
,恢复结果,然后在结果集合上执行所需的假设筛选器

isUser(x.ID)
因为,我猜想,它以某种方式执行更复杂的验证


重要的是,为了避免数据传输延迟,从数据库中得到的结果要尽可能少。

您可以调用以下代码:

var emp = from x in gp.Employees
                  where x.ActivtyStatus == 0
                  orderby x.ID

并获得有效的结果,因为每个linq表达式都可以通过linq to sql应用于员工。然后,您可以将emp强制转换为列表或and数组,并使用您的方法isUser筛选集合。

作为替代解决方案,您可以在LINQ查询中在线调用任何sql UDF(用户定义函数),如
gp.isUser(x.ID)
,因此您可以将此函数定义为sql UDF,例如:

 CREATE FUNCTION isUser(@id int)
 RETURNS bit;
 AS
 BEGIN
    if exists(select * from Users where userId = @id)
    begin 
          return 1; --true the user exists;
    else 
          return 0; --false doesn't exist;
    end
 end
然后,必须创建包含其他表和过程定义映射的。然后,您可以在LINQ查询中执行以下操作:

var emp = from x in gp.Employees
              let k = gp.isUser(x.ID)
              where x.ActivtyStatus == 0
              && gp.isUser(x.ID) != 1
              orderby x.ID
              select new
              {
                  ID = x.ID,
                  Name = x.FirstName + " " + x.MiddleName + " " + x.LastName
              };

Employees.ID的可能值是什么?为什么一个可以解析为int的字符串意味着一个雇员是一个用户?@spender关于一个用户将拥有像“11064”这样的ID的部分,团队将拥有“oss”。关于我来的原因,系统是在大平原上建立的:(旧系统在获得一次收益后就会运行。如果检查字段中是否只有数字就足够了,您可以使用类似的表达式。我已经用信息和链接更新了我的答案。+1在给定链接处使用
IsNumeric
的UDF可能是这种情况下的最佳解决方案。如果它经常使用视图或计算列,这里可能会有用。