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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:44:43+00:00 2026-05-30T05:44:43+00:00

I have a model Course, which has several many to many relationships like Age

  • 0

I have a model Course, which has several many to many relationships like Age or Time.

I have this query:

string IDs = "1,2,3"
string[] IDList = IDs.Split(',');

return (from x in entities.Course
       where x.Ages.Where(val => IDList.Contains(val.ID.ToString())).Count() == IDList.Count()
       select x);

And I need to set the same query for Time and several other properties as in:

string IDs = "1,2,3"
string[] IDList = IDs.Split(',');

return (from x in entities.Course
       where x.Times.Where(val => IDList.Contains(val.ID.ToString())).Count() == IDList.Count()
       select x);

How can I make the query more dynamic so I don’t have multiple similar queries?

Thanks

  • 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-30T05:44:45+00:00Added an answer on May 30, 2026 at 5:44 am

    You could make a method that accepts an Expression (depeneding on your data type) and run the query that way. You’ll need to make your Ages, Time, etc implement a specific interface for it to work.

    For example, assuming that you are using EF and your model is Code First using DbSets, you could make this:

    public interface IObject
    {
        int ID { get; set; }
    }
    
    public class Age : IObject
    {
        public int ID { get; set; }
    
        // The rest of the data
    }
    
    public class Time : IObject
    {
        public int ID { get; set; }
    
        // The rest of the data
    }
    
    public class Course
    {
        public virtual ICollection<Age> Ages { get; set; }
        public virtual ICollection<Time> Times { get; set; }
    }
    
    public class CourseContext : DbContext
    {
        public DbSet<Course> Course { get; set; }
    }
    
    public class Test
    {
        public IQueryable<Course> GetCourses(Expression<Func<Course, ICollection<IObject>>> exp)
        {
            var entities = new CourseContext(); 
            string IDs = "1,2,3";
            string[] IDList = IDs.Split(',');
    
            var c = exp.Compile();
    
            return entities.Course.Where(x => c.Invoke(x).Count(val => IDList.Contains(val.ID.ToString())) == IDList.Count());
        }
    
        public void TestMethod()
        {
            var times = GetCourses(c => (ICollection<IObject>)c.Times);
            var ages = GetCourses(c => (ICollection<IObject>)c.Ages);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data model in Doctrine/symfony. I have a 'Course' which has many
I have model like this: scope :search_posts, lambda { |query| mega_posts.where( title LIKE ?
I have a User model which has many projects and a Project model which
I have Course's model and the course consist of several file (stored only link
I have model Foo which has field bar. The bar field should be unique,
I have situation where a single model needs to have three foreign ids. This
I have a course model, which has_many timeslots. In the courses model, I have
I have a ViewModel that looks like this: public class CreateReviewViewModel { public string
I have an Invoice model which belongs to Customer (and of course Customer has_many
I have several tables in my ERD which which I would like to combine

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.