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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:14:53+00:00 2026-05-18T09:14:53+00:00

I have a Generic repository and are trying to cast the .Returns to a

  • 0

I have a Generic repository and are trying to cast the .Returns to a Expression but it refuse… My code is following:

public RepositoryTest()
{
    IList<MockObjectSet> mocks = new List<MockObjectSet>()
    {
        new MockObjectSet { FirstName = "Beta", LastName = "Alpha", Mobile = 12345678 },
        new MockObjectSet { FirstName = "Alpha", LastName = "Beta", Mobile = 87654321 }
    };

    var mockRepository = new Mock<IRepository<MockObjectSet>>();

    mockRepository.Setup(x => x.GetBy(It.IsAny<Expression<Func<MockObjectSet, bool>>>()))
        .Returns((Expression<Func<MockObjectSet, bool>> predicate) => mocks.Where(predicate).ToList());

}

It just say

Delegate System.Func<System.Collections.Generic.IEnumerable<expWEBCRM.Tests.Repositories.MockObjectSet>> does not take 1 arguments

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-18T09:14:54+00:00Added an answer on May 18, 2026 at 9:14 am

    You need to explicitly specify the type parameters of the Returns overload like so:

    mockRepository.Setup(x => x.GetBy(It.IsAny<Expression<Func<MockObjectSet, bool>>>()))
            .Returns<Expression<Func<MockObjectSet, bool>>>(predicate => mocks.Where(predicate).ToList());
    

    EDIT The repository takes an expression and uses it on a IQueryable. The mock data source is actually an IEnumerable. The difference in the LINQ interface is one takes a lambda, the one an expression:

    IQueryable<T>.Where(Expression<Func<T,bool>>);
    IEnumerable<T>.Where(Func<T,bool>);
    

    What happens in this scenario is trying to call IEnumerable.Where with Expression<Func<T,bool>>. The easiest way to fix this is to have the source collection as IQueryable:

    public RepositoryTest()
    {
        IQueryable<MockObjectSet> mocks = new List<MockObjectSet>()
        {
            new MockObjectSet { FirstName = "Beta", LastName = "Alpha", Mobile = 12345678 },
            new MockObjectSet { FirstName = "Alpha", LastName = "Beta", Mobile = 87654321 }
        }.AsQueryable();
    
        var mockRepository = new Mock<IRepository<MockObjectSet>>();
    
        mockRepository.Setup(x => x.GetBy(It.IsAny<Expression<Func<MockObjectSet, bool>>>()))
            .Returns<Expression<Func<MockObjectSet, bool>>>(predicate => mocks.Where(predicate).ToList());
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following generic repository: public class GenericRepository<TEntity> where TEntity : class {
I have the following function in my generic repository which works fine: public IQueryable<T>
I have seen generic repository pattern here . I am trying to add generic
I have this method to get a generic repository out of a dictionary: public
I have a repository class that inherits from a generic implementation: public namespace RepositoryImplementation
I currently have a complete generic repository but I'm missing one feature and that
Hello im trying to build a generic repository on top of linq2sql. I have
I have a generic repository an I am trying to add a GetById method
I have a generic repository: public class GenericRepository<TEntity> : AbstractRepository<TEntity>, IRepository<TEntity> where TEntity :
I have the following problem: I have a generic EF repository using DbContext designed

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.