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
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();
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