Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3314712
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:15:55+00:00 2026-05-17T22:15:55+00:00

I have a method called GetAge(DateTime birthDay). I want to use this method in

  • 0

I have a method called GetAge(DateTime birthDay). I want to use this method in the Linq Query by passing the birthday and based on the returned age value need to perfrom some logic.

I want below query in LINQ format –

  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"

Immediate help would be highly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T22:15:55+00:00Added an answer on May 17, 2026 at 10:15 pm
    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"));
    

    The .AsEnumerable is required if you’re attempting to query an SQL database as the GetAge method in your code will have no translation to SQL. In that case the call to .AsEnumerable retrieves the results of the query up to that point and you’re then working with local objects on which your method can operate.

    If you don’t want to retrieve all the results at that point because the number of records is large you could always replicate the logic from the method you want to call in your query (I’m guessing at the logic here):

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

    Because the operations are all available in SQL this will work.

    If the method you’re trying to call is particularly complex you can always move it to an extension method that takes an IQueryable and returns an IQueryable. The contents of the method will still need to have a valid translation to SQL but it will help hide more complicated logic.

    For example the above query could be made to look like this:

    context.Customers.WhoAreValidByAge();
    

    Where WhoAreValidByAge is defined as:

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

    If the logic contained in your method doesn’t translate to SQL for some reason though you have no choice but to convert the results set to LinqToObjects. In that case I’d suggest filtering the results as much as possible in SQL before calling AsEnumerable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose that I have a method called doSomething() and I want to use this
I have a method called revisions, and I want to be able use the
I have this method Verify_X which is called during databind for a listbox selected
I have an interface called Dictionary which has a method insert() . This interface
I have a method called funct which i want to have as my callback
If you have a method called set in a class and want to create
I have this method called LoadToDictionary. It accepts a dictionary and a filepath. Currently,
My data access classes often have a method called loadDataToEntity or something similar. This
I have a class called EventConsumer which defines an event EventConsumed and a method
I have a data tier select method that returns a datatable. It's called from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.