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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:09:44+00:00 2026-06-15T19:09:44+00:00

I am Working on a MVC4 project and I am in need to create

  • 0

I am Working on a MVC4 project and I am in need to create a new list of objects by comparing two lists. db below refers to the DbContext object.

In the project that I work, I am in need of comparing two lists. The first has all the subjects offered in a semester. The other has the subjects selected by the student.

List<Subject> subjects = db.Subjects;
//...
List<Subject> semesterSubjects = student.Subjects;

Out of this two lists I need to create a list of items that state if or not each an every item is selected. It looks like this.

class SubjectSelection
{
public int ID { get; set; }
public String Title { get; set; }
public bool IsSelected { get; set; }
}

What I need now is to compare subjects & semesterSubjects and get a IEnumerable<SubjectSelection>

Could I use Linq to get such a result and how?

  • 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-15T19:09:46+00:00Added an answer on June 15, 2026 at 7:09 pm

    Best way is to do this job on the server side. Select ids from your semester subjects:

    var selectedIDs = semesterSubjects.Select(s => s.ID).ToList();
    

    And pass them to query:

    var query = db.Subjects.Select(s => new SubjectSelection()
                         {
                            ID = s.ID,
                            Title = s.Title,
                            IsSelected = selectedIDs.Contains(s.ID)
                         });
    

    If you are using same db context instance to retrieve both semester subjects list and subjects list, then you can simply use:

    var query = subjects.Select(s => new SubjectSelection()
                         {
                            ID = s.ID,
                            Title = s.Title,
                            IsSelected = semesterSubjects.Contains(s)
                         });
    

    Entity Framework implements identity map pattern, which means it returns same in-memory instance of subject object, which returned by different queries.

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

Sidebar

Related Questions

I am working on an MVC4 razor project. I have a list of role
I have a MVC 4 project I am currently working on and need some
Client side Validation and ValidationSummary is working fine for my project (MVC4 + Razor
I am working on a project ASP.net 4, MVC4(RC) using visual studio 11. I
I am working with nuget and find it excellent with my MVC4 project. I
I have an MVC4 project, and I am trying to get it working on
I am currently working through my first ASP.MVC4 project in VS2012 and I was
I'm working on an MVC4 project that has site controllers and API controllers. How
I am working on a mvc4 project where I use a Kendo Grid. I
Anyone had this issue? I opened an MVC4 project and all was working fine

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.