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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:52:46+00:00 2026-05-13T01:52:46+00:00

Probably this question was already asked before, but my google-fu and SO-Search did not

  • 0

Probably this question was already asked before, but my google-fu and SO-Search did not get me what what I was looking for.

I have a custom class, and a custom class comparer (for checking the equality of the class) implemented with IEqualityComparer.

public class Person
{
        public string Name { get; set; }
        public bool Flag { get; set; }
}

public class PersonComparer : IEqualityComparer<Person>
{
        #region IEqualityComparer<Person> Members

        public bool Equals(Person x, Person y)
        {
            //case insensitive compare
            return string.Equals(x.Name, y.Name, StringComparison.OrdinalIgnoreCase);
        }

        public int GetHashCode(Person obj)
        {
            return base.GetHashCode();
        }

        #endregion
 }

and in the main portion of the code I have 2 lists “source” and “target”

Person bob = new Person() { Name = "Bob" };
Person sam = new Person() { Name = "Sam" };
Person andy = new Person() { Name = "Andy" };
Person thomas = new Person() { Name = "Thomas" };
Person jimmy = new Person() { Name = "Jimmy" };
Person sam2 = new Person() { Name = "sam" }; // note the lower case
Person jane = new Person() { Name = "Jane" };
List<Person> source = new List<Person>() { bob, sam, andy, thomas };
List<Person> target = new List<Person>() { sam2, andy,jane };

what I want to do

  1. update source list to only contain sam and andy, as bob and thomas are not in the target list. I did this

    source = (from p in source where (from t in target select t)
    .Contains(p, new PersonComparer())
    select p).ToList();

  2. In the target I should “Flag” sam2 and andy to true and jane is flagged as “false” by default, I should not change it.

I tried using this, but this removes “jane” from target

//sets  sam2 & andy to true, removes Jane
target = (from p in target.Select(t => { t.Flag = true; return t; })
          where (from s in source
          select s).Intersect(select p).ToList();

Can any LINQ guru tell me what I am doing wrong ?

3.Is there a better way to write Query 1 ?

4.And finally a trivial question: how exactly do you say “=>” when you are talking to a fellow coder over the phone

  • 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-13T01:52:46+00:00Added an answer on May 13, 2026 at 1:52 am

    As Sander has pointed out, LINQ is for querying, not updating.

    However, to answer the questions… Your original query of

    source = (from p in source where (from t in target select t)
        .Contains(p, new PersonComparer()) select p).ToList();
    

    would be much more simply written as:

    source = source.Intersect(target, new PersonComparer()).ToList();
    

    Having said that, you need to update PersonComparer as recursive mentioned. It should be something like this:

    public int GetHashCode(Person obj)
    {
        return obj == null ? 0 
             : StringComparer.OrdinalIgnoreCase.GetHashCode(obj.Name);
    }
    

    I’m afraid I don’t really understand your second query particularly well… but if you want to change the existing objects, I’d suggest a foreach loop instead of trying to use LINQ. Queries with side-effects are generally a bad idea.

    You may mean something like:

    // You may want to make some singleton instance available, as this has no state
    PersonComparer comparer = new PersonComparer();
    foreach (Person person in target)
    {
        if (source.Contains(person, comparer))
        {
            person.Flag = true;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Probably someone has already asked this question but I'm not sure what I'm looking
I realize this question has probably been asked numerous times, but I have not
I half realize that this question is probably asked already, but I'm not familiar
Okay, I kinda asked this question already, but noticed that i might have not
this question has probably been asked before, but i'm stuck and i've tried a
I know this question has probably been asked already but I would really like
I know this question has already been asked a lot, but I don't get
First of all there is probably a question like this already but i couldn't
I think this question was already asked, but I couldn't find a solution which
Alright, I know questions like this have probably been asked dozens of times, but

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.