I'm doing LINQ to Sql. To have a good performance I'm trying to filter some records on the SQL Server side itself. So I want to use a user defined function (UDF) GiveWordCount available in my database. Based on this post here is what I'm trying on the C# side to use that UDF.
Declare a dummy function body so that C# code can compile:
[Function(Name = "dbo.GiveWordCount", IsComposable = true)]
public static int GiveWordCount([Parameter(Name="@inputValue",DbType="nvarchar(4000)")]string inputValue)
{
throw new NotSupportedException("Direct calls are not supported.");
}
Then I call this function in my LINQ query as show below. Call to 'GiveWordCount' function should get converted by LINQ to Sql provider into an equivalent in-built sql server function before the final SQL query gets fired onto the database. Instead it results in an error:
Method 'Int32 GiveWordCount(System.String)' has no supported translation to SQL.
Here is my main function:
static void Main(string[] args)
{
DataContext dataContext = new DataContext("data source=.;initial catalog=businessLinqToSql;integrated security=True;MultipleActiveResultSets=True");
Table<Customer> customers = dataContext.GetTable<Customer>();
IEnumerable<string> b = customers.Where(customer => GiveWordCount(customer.Name) <= 2).Select(x => x.Name);
foreach (string element in b) Console.WriteLine(element);
}
Here is my Customer Class:
[Table]
public class Customer
{
[Column]
public string Name { get; set; }
}
SqlFunctionsclass or not when I started getting this error. It seems I will have to take some other route. I tried even this for my original problem statement but I get very same error which I've mentioned: stackoverflow.com/questions/3865310/… I'm using sql-clr integration to create my UDF.SqlFunctionsisSqlMethods- but it's not as broad. I don't know about UDFs...