在Linq Query中调用类方法

CHa*_*ike 7 c# linq

我有一个名为GetAge(DateTime birthDay)的方法.我想在Linq Query中使用这个方法,通过传递生日并根据返回的年龄值需要执行一些逻辑.

我想在LINQ格式下面查询 -

  from customer in contetx.Customer where 
  if GetAge(customer.BirthDate) > 20 and customer.accountType="Savings"
  or
  if(GetAge(customer.BirthDate) > 40 and customer.AccountType="Current"
Run Code Online (Sandbox Code Playgroud)

立即的帮助将受到高度赞赏.

Dar*_*rov 11

var customers = from customer in contetx.Customer 
                let age = GetAge(customer.BirthDate)
                where (age > 20 && customer.accountType == "Savings") ||
                      (age > 40 && customer.accountType == "Current")
                select customer;
Run Code Online (Sandbox Code Playgroud)


Dav*_*wns 7

context.Customer
       .AsEnumerable()  // because the method has no translation to SQL
       .Where(customer => (GetAge(customer.BirthDate) > 20 && customer.AccountType == "Savings")
                       || (GetAge(customer.BirthDate) > 40 && customer.AccountType == "Current"));
Run Code Online (Sandbox Code Playgroud)

.AsEnumerable,如果你试图查询SQL数据库在代码中GetAge方法没有任何转换为SQL是必需的.在这种情况下,调用将.AsEnumerable检索查询结果直到该点,然后您将使用您的方法可以在其上运行的本地对象.

如果您不想在那时检索所有结果,因为记录数量很大,您总是可以从您想要在查询中调用的方法复制逻辑(我在这里猜测逻辑):

context.Customer.Select(c => new { Customer = c, Age = (DateTime.Today.Year - c.BirthDate.Year) }
                .Where(c => (c.Age > 20 && c.Customer.AccountType == "Savings")
                         || (c.Age > 40 && c.Customer.AccountType == "Current"))
                .Select(c => c.Customer);
Run Code Online (Sandbox Code Playgroud)

因为这些操作都在SQL中可用,所以这将起作用.

如果您尝试调用的方法特别复杂,您可以随时将其移动到一个带有IQueryable和返回的扩展方法IQueryable.该方法的内容仍然需要有一个有效的SQL转换,但它将有助于隐藏更复杂的逻辑.

例如,上面的查询可以看起来像这样:

context.Customers.WhoAreValidByAge();
Run Code Online (Sandbox Code Playgroud)

在哪里WhoAreValidByAge定义为:

public static IQueryable<Customer> WhoAreValidByAge(this IQueryable<Customer> customers)
{
    cusomters.Select(c => new { Customer = c, Age = (DateTime.Today.Year - c.BirthDate.Year) }
             .Where(c => (c.Age > 20 && c.Customer.AccountType == "Savings")
                      || (c.Age > 40 && c.Customer.AccountType == "Current"))
             .Select(c => c.Customer)
}
Run Code Online (Sandbox Code Playgroud)

如果您的方法中包含的逻辑由于某种原因没有转换为SQL,尽管您别无选择,只能将结果集转换为LinqToObjects.在这种情况下,我建议在调用之前尽可能在SQL中过滤结果AsEnumerable.

  • 我不认为有必要调用`AsEnumerable()`. (2认同)