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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:03:58+00:00 2026-06-10T17:03:58+00:00

I am using EF 4 with repository pattern which has the generic query method

  • 0

I am using EF 4 with repository pattern which has the generic query method shown below:

public IEnumerable<T> Query(Expression<Func<T, bool>> filter) 
    { 
        return objectSet.Where(filter); 
    } 

I know I can select a complete object like this:

context.PeriodRepository.Query(a => a.EntityId == selectedEntityId); 

But I want to pass a Linq query that returns it as type instead of IEnumerable<type> using a LINQ expression without changing the method. Please advise me how to do that.

  • 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-06-10T17:03:59+00:00Added an answer on June 10, 2026 at 5:03 pm

    A Query describes the operations performed on the list; you must perform the query to get the result(s).

    Using .ToList()/.ToArray() will return all items that match the query. Use .First() to get the first item that matches or .FirstOrDefault() to get the first item or the default value for no matches.

    The default value for a class is null.

    I think your code should be like this:

    var myMatch = context.PeriodRepository
       .Query(a => a.EntityId == selectedEntityId)
       .First();
    

    Also note that, First() will throw an exception when there is no match.

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

Sidebar

Related Questions

I am using a standard repository pattern with a service layer which calls the
I am trying to implement generic repository pattern using LINQ to SQL data context.
I'm trying to implement the repository pattern using a generic repository like the one
I am using the Generic Repository pattern to abstract my EF 4.1 DbContext. I
I have an MVC-based site, which is using a Repository/Service pattern for data access.
I am using a Repository pattern with filters in my MVC application. The project
I'm getting into using the Repository Pattern for data access with the Entity Framework
What is a typical approach to using the repository pattern with .NET 1.1 (C#)?
I have these classes and I'm using a Repository pattern and trying to get
I have a situation where I will be using a repository pattern and pulling

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.