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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:50:21+00:00 2026-05-31T03:50:21+00:00

I’m using System.Data.Objects.EntityFunctions.TruncateTime method to get date part of a datetime in my query:

  • 0

I’m using System.Data.Objects.EntityFunctions.TruncateTime method to get date part of a datetime in my query:

if (searchOptions.Date.HasValue)
    query = query.Where(c => 
        EntityFunctions.TruncateTime(c.Date) == searchOptions.Date);

This method (I believe the same applies to other EntityFunctions methods) cannot be executed outside of LINQ to Entities. Executing this code in a unit test, which effectively is LINQ to objects, causes a NotSupportedException to be thrown:

System.NotSupportedException : This function can only be invoked from
LINQ to Entities.

I’m using a stub for a repository with fake DbSets in my tests.

So how should I unit test my query?

  • 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-31T03:50:22+00:00Added an answer on May 31, 2026 at 3:50 am

    You can’t – if unit testing means that you are using a fake repository in memory and you are therefore using LINQ to Objects. If you test your queries with LINQ to Objects you didn’t test your application but only your fake repository.

    Your exception is the less dangerous case as it indicates that you have a red test, but probably actually a working application.

    More dangerous is the case the other way around: Having a green test but a crashing application or queries which do not return the same results as your test. Queries like…

    context.MyEntities.Where(e => MyBoolFunction(e)).ToList()
    

    or

    context.MyEntities.Select(e => new MyEntity { Name = e.Name }).ToList()
    

    …will work fine in your test but not with LINQ to Entities in your application.

    A query like…

    context.MyEntities.Where(e => e.Name == "abc").ToList()
    

    …will potentially return different results with LINQ to Objects than LINQ to Entities.

    You can only test this and the query in your question by building integration tests which are using the LINQ to Entities provider of your application and a real database.

    Edit

    If you still want to write unit tests I think you must fake the query itself or at least expressions in the query. I could imagine that something along the lines of the following code might work:

    Create an interface for the Where expression:

    public interface IEntityExpressions
    {
        Expression<Func<MyEntity, bool>> GetSearchByDateExpression(DateTime date);
        // maybe more expressions which use EntityFunctions or SqlFunctions
    }
    

    Create an implementation for your application…

    public class EntityExpressions : IEntityExpressions
    {
        public Expression<Func<MyEntity, bool>>
            GetSearchByDateExpression(DateTime date)
        {
           return e => EntityFunctions.TruncateTime(e.Date) == date;
           // Expression for LINQ to Entities, does not work with LINQ to Objects
        }
    }
    

    …and a second implementation in your Unit test project:

    public class FakeEntityExpressions : IEntityExpressions
    {
        public Expression<Func<MyEntity, bool>>
            GetSearchByDateExpression(DateTime date)
        {
            return e => e.Date.Date == date;
           // Expression for LINQ to Objects, does not work with LINQ to Entities
        }
    }
    

    In your class where you are using the query create a private member of this interface and two constructors:

    public class MyClass
    {
        private readonly IEntityExpressions _entityExpressions;
    
        public MyClass()
        {
            _entityExpressions = new EntityExpressions(); // "poor man's IOC"
        }
    
        public MyClass(IEntityExpressions entityExpressions)
        {
            _entityExpressions = entityExpressions;
        }
    
        // just an example, I don't know how exactly the context of your query is
        public IQueryable<MyEntity> BuildQuery(IQueryable<MyEntity> query,
            SearchOptions searchOptions)
        {
            if (searchOptions.Date.HasValue)
                query = query.Where(_entityExpressions.GetSearchByDateExpression(
                    searchOptions.Date));
            return query;
        }
    }
    

    Use the first (default) constructor in your application:

    var myClass = new MyClass();
    var searchOptions = new SearchOptions { Date = DateTime.Now.Date };
    
    var query = myClass.BuildQuery(context.MyEntities, searchOptions);
    
    var result = query.ToList(); // this is LINQ to Entities, queries database
    

    Use the second constructor with FakeEntityExpressions in your unit test:

    IEntityExpressions entityExpressions = new FakeEntityExpressions();
    var myClass = new MyClass(entityExpressions);
    var searchOptions = new SearchOptions { Date = DateTime.Now.Date };
    var fakeList = new List<MyEntity> { new MyEntity { ... }, ... };
    
    var query = myClass.BuildQuery(fakeList.AsQueryable(), searchOptions);
    
    var result = query.ToList(); // this is LINQ to Objects, queries in memory
    

    If you are using a dependency injection container you could leverage it by injecting the appropriate implementation if IEntityExpressions into the constructor and don’t need the default constructor.

    I’ve tested the example code above and it worked.

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I want to construct a data frame in an Rcpp function, but when I

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.