I have the following code:
public void SalesCount(string customerId)
{
..
..
return ...;
}
var resultQuery = dataContext.Customers
.Where (c => c.Name == "Alugili")
.Where (c => SalesCount(c.CustomerId) < 100);
When I execute resultQuery I get a translation to SQL exception.
I need to call SalesCount in the Where can I do that is there any workaround for this problem!
var q = (from a in v.A join b in v.B on a.i equals b.j where a.k == "aaa" && a.h == 0 select new {T = a.i, S = someMethod(a.z). ToString()}) return q; The line S = someMethod(a.z).
To execute a LINQ to Entities query against the Entity Framework, the LINQ query must be converted to a command tree representation that can be executed against the Entity Framework.
Step 1: Create an entity class which inherits “DbContext” class. Step 2: The following is the structure of the database with table and stored procedure. Step 3: Create a class to store the returned tabular value. Step 4: Create an object for the entity above and method to call a function.
LINQ to SQL translates the queries you write into equivalent SQL queries and sends them to the server for processing. More specifically, your application uses the LINQ to SQL API to request query execution. The LINQ to SQL provider then transforms the query into SQL text and delegates execution to the ADO provider.
Simply you can't. You can't execute C# code on the SQL server directly, you can use only Expressions
and some special recognized functions...
Unless you transform your query (at least partially) in a LINQ-to-Objects...
var resultQuery = dataContext.Customers
.Where (c => c.Name == "Alugili")
.AsEnumerable()
.Where (c => SalesCount(c.CustomerId) < 100);
Be aware that the last Where
will be executed on the client side, so many useless rows will be fetched from the DB.
Try
var resultQuery = dataContext.Customers
.Where (c => c.Name == "Alugili")
.ToArray()
.Where (c => SalesCount(c.CustomerId) < 100);
But then the second Where
is not run as SQL but locally - all customers with the name "Alugili" are pulled from the DB...
Otherwise you have to write out your method directly in the where method as lambda expression.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With