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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:54:58+00:00 2026-06-01T08:54:58+00:00

I have a method which takes an array of strings as parameter and queries

  • 0

I have a method which takes an array of strings as parameter and queries against a collection property which is also a collection of strings. If that property has one of the values inside the string array passed as parameter, it should be returned.

Here is my code:

public IEnumerable<BlogPost> GetAll(string[] tags, 
    bool includeUnapprovedEntries = false) {

    foreach (var tag in tags) {
        foreach (var blogPost in GetAll(includeUnapprovedEntries).
            ToList().Where(x => x.Tags.Any(t => t == tag))) {
            yield return blogPost;
        }
    }
}

Note:

Here is the complete code:

https://github.com/tugberkugurlu/MvcBloggy/blob/master/src/MvcBloggy.Data/DataAccess/SqlServer/BlogPostRepository.cs

This does the job but it just doesn’t seem right. I could have made this better with some extension methods but couldn’t figure out what would do the trick and make this implementation right.

Any idea?

  • 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-01T08:54:59+00:00Added an answer on June 1, 2026 at 8:54 am

    How about this:

    public IEnumerable<BlogPost> GetAll(string[] tags,
        bool includeUnapprovedEntries = false) {
    
        return GetAll(includeUnapprovedEntries)
            .Where(x => x.Tags.Any(t => tags.Contains(t));
    }
    

    You may want to call ToList() to materialize the result. Note that this will (hopefully!) result in an IN query in SQL; if you have a large number of tags, I wouldn’t be surprised if that failed. (I don’t know how the Entity Framework handles that situation.) I believe it should be okay with smaller numbers of tags though.

    Note that whether or not this is supported may depend on the version of the entity framework you’re using; I seem to remember that some transformations like this (using Contains on a “local” collection) to translate to IN in SQL) have improved over time. Make sure you develop against the same version you’ll be deploying against 🙂

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

Sidebar

Related Questions

I have a method that takes an array of strings and joins them so
I have a Java method which takes a String as parameter and returns a
I have this method which takes a varargs of Strings, creates a List out
I have a method which takes params object[] such as: void Foo(params object[] items)
i have a method which takes in a DotNetOpenAuth (formally known as DotNetOpenId) Response
I have a method which takes a directory path as a string. In the
My problem is the following. I have a method which simply takes an XML
I have an controller that returns JSON. It takes a form, which validates itself
I have C function that takes string pointer as a parameter. This functions returns
I have created a webservice which takes a DTO as a parameter. And DTO

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.