Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trying to call a method in the where of a linq statment

Below is the code I'm using but it replies with

Method 'Boolean isUser(System.String)' has no supported translation to SQL.

Any help? Btw I'm using linq to SQL data source

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

I found a solution to query the result of the first query as objects but is that good cause I will passing through my data twice.


the updated code that worked in the end after using the like as advised by 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();
        }
    }
like image 564
Shehab Fawzy Avatar asked Jan 11 '12 09:01

Shehab Fawzy


People also ask

What is where clause in LINQ?

Where Clause in Query Syntax: The where clause is used to filter the query according to the given condition. You can provide a condition to where clause using lambda expression or by using Func delegate type. Where clause supports query syntax in both C# and VB.Net languages.

Can we use multiple where clause in LINQ query?

A single query expression may have multiple where clauses.


2 Answers

Linq-to-sql translates the query into SQL. It only knows how to translate a limited set of built in functions. You have to rewrite your query to not include a function of your own.

A complete list of the linq-to-sql supported functions and operators can be found at MSDN.

You can use SqlMethods.Like() to check if the field only contains digits. See T-SQL IsNumeric() and Linq-to-SQL for an example.

like image 103
Anders Abel Avatar answered Sep 22 '22 13:09

Anders Abel


The problem you're having is that because the query needs to be ran on the database, you can only use things that will work on the database, the C# code in your isUser method can't run on the database.

You'll have to re-write it without using that function. Perhaps you have a table listing users by IDs you could join on?

like image 35
George Duckett Avatar answered Sep 20 '22 13:09

George Duckett