How can I call local method in Linq to Entities query?

后端 未结 4 1543
感动是毒
感动是毒 2020-12-10 14:51

I have the following code:

public void SalesCount(string customerId)
{
  ..
  ..
  return ...;
}

var resultQuery = dataContext.Customers
.Where (c => c.N         


        
相关标签:
4条回答
  • 2020-12-10 15:21

    Add && between two where condition..It will work

    0 讨论(0)
  • 2020-12-10 15:36

    yes, you can.

    but you should change return type for your function:

    public **int** SalesCount(string customerId)
    {
      ..
      ..
      return 500;
    }
    
    
    var resultQuery = dataContext.Customers.AsEnumerable()
                     .Where (c => c.Name == "Alugili" && SalesCount(c.CustomerId) < 100);
    
    0 讨论(0)
  • 2020-12-10 15:42

    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.

    0 讨论(0)
  • 2020-12-10 15:42

    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.

    0 讨论(0)
提交回复
热议问题