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 3602878
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:48:07+00:00 2026-05-18T20:48:07+00:00

I have two Entity Framework 4 Linq queries I wrote that make use of

  • 0

I have two Entity Framework 4 Linq queries I wrote that make use of a custom class method, one works and one does not:

The custom method is:

public static DateTime GetLastReadToDate(string fbaUsername, Discussion discussion)
{
 return (discussion.DiscussionUserReads.Where(dur => dur.User.aspnet_User.UserName == fbaUsername).FirstOrDefault() ?? new DiscussionUserRead { ReadToDate = DateTime.Now.AddYears(-99) }).ReadToDate;
}

The linq query that works calls a from after a from, the equivalent of SelectMany():

  from g in oc.Users.Where(u => u.aspnet_User.UserName == fbaUsername).First().Groups
  from d in g.Discussions
  select new
  {
   UnReadPostCount = d.Posts.Where(p => p.CreatedDate > DiscussionRepository.GetLastReadToDate(fbaUsername, p.Discussion)).Count()
  };

The query that does not work is more like a regular select:

  from d in oc.Discussions
  where d.Group.Name == "Student"
  select new 
  {
   UnReadPostCount = d.Posts.Where(p => p.CreatedDate > DiscussionRepository.GetLastReadToDate(fbaUsername, p.Discussion)).Count(),
  };

The error I get is:

LINQ to Entities does not recognize the method 'System.DateTime GetLastReadToDate(System.String, Discussion)' method, and this method cannot be translated into a store expression.

My question is, why am I able to use my custom GetLastReadToDate() method in the first query and not the second? I suppose this has something to do with what gets executed on the db server and what gets executed on the client? These queries seem to use the GetLastReadToDate() method so similarly though, I’m wondering why would work for the first and not the second, and most importantly if there’s a way to factor common query syntax like what’s in the GetLastReadToDate() method into a separate location to be reused in several different other LINQ queries.

Please note all these queries are sharing the same object context.

  • 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-18T20:48:08+00:00Added an answer on May 18, 2026 at 8:48 pm

    I think your better of using a Model Defined Function here.

    Define a scalar function in your database which returns a DateTime, pass through whatever you need, map it on your model, then use it in your LINQ query:

    from g in oc.Users.Where(u => u.aspnet_User.UserName == fbaUsername).First().Groups
      from d in g.Discussions
      select new
      {
       UnReadPostCount = d.Posts.Where(p => p.CreatedDate > myFunkyModelFunction(fbaUsername, p.Discussion)).Count()
      };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have two Entity Framework queries, one with Include one with standalone query. Here
I have Two Many-To-Many tables which I need to write a Entity Framework Linq
I have two Entity Framework objects with a one-to-many relationship: widget(parent) and widgetNail(child) Widget
I have two entity classes annotated in the following way @Entity class A {
I have two tables that are considered a single entity in my domain model.
I have two tables in my database schema that represent an entity having a
A have two entities. For example timing settings and orders. @Entity public class TimingSettings{
I have a database and I am using the Entity Framework with Linq 2
I use c#, linq and EF4. I have two tables in my DataBase represented
I have two tables. I am updating those tables using entity framework. here is

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.