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

  • Home
  • SEARCH
  • 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 1086609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:50:05+00:00 2026-05-16T22:50:05+00:00

In the database of an application I’m using, there is a record that has

  • 0

In the database of an application I’m using, there is a record that has a “Date” field. In many places in the code, we need to translate that into a Fiscal Year for grouping, selecting, and filtering. So here are a few examples of what a query might look like:

var query = from r in DB.records
            let fy = r.Date.Month >= 10 ? r.Year + 1 : r.Year
            where fy = 2010
            select r;

Now I know I could write an Expression method to use the same filter, but what if I want to use it for selecting and grouping? I don’t want to have to write the second line over and over in all of the queries. It would be nice to just be able to do:

var query = from r in DB.records
            let fy = r.Date.GetFY()
            where fy = 2010
            group r by fy into g
            select g.Count();

That way, different clients could have different FY definitions and such, and have it all be defined in the same place. Is there any way to do this? Writing a normal GetFY method just throws the “Can’t translate to SQL” (or whatever it actually is) exception. Obviously, this can be translated to SQL on some level, I just don’t know if it’s possible to recreate in an easy way for LINQ to SQL to reuse.

Thanks in advance.

  • 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-16T22:50:05+00:00Added an answer on May 16, 2026 at 10:50 pm

    If you can write it as an SQL function, you can create a dummy C# function to use in the LINQ code, and tell LINQ to map it to your SQL function. The mapping is done through an attribute.

    You would need to do something like:

    [Function(Name = "udf_GetFY", IsComposable = true)]
    public int  getFY(DateTime t)
    {
        return 5; // value doesn't matter
    }
    

    and then:

     var query = from r in DB.records 
            let fy = GetFY(r.Date) 
            where fy = 2010 
            group r by fy into g 
            select g.Count();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A database application that I'm currently working on, stores all sorts of settings in
I have written a database application using a binary file as storage. it is
I'm writing a database application that connects to SQL server. I'd like to implement
I am working on a database application that runs on various independent servers. Each
A new client of mine has a small VB/Access database application written in 2002
I need to build a simple, single user database application for Windows. Main requirements
In my database application I sometimes have to deal with null strings in the
I am going to write a database application for the camp I work for.
I'm starting a new database application and I wonder if it would be better
I am considering writing a simple database application for my wife, but I'm hung

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.