c# - Left join Lists with Linq and detect multiple matches -
my goal compare lists , update values on list values list b. along that, want work left join , keep values list if didn't updated , detect multiple matches.
what have tried far below.
the setup
public class person { public string firstname { get; set; } public string lastname { get; set; } public string updateme { get; set; } public static list<person> createlist() { return new list<person> { new person { lastname = "barton", firstname = "clint" }, new person { lastname = "stark", firstname = "tony" }, new person { lastname = "parker", firstname = "peter" } }; } public static list<person> createlisttwo() { return new list<person> { new person { lastname = "barton", firstname = "clint", updateme = "updated"}, new person { lastname = "stark", firstname = "tony", updateme = "updated"}, }; } } public class firstlastnamematch : iequalitycomparer<person> { public bool equals(person x, person y) { return x.firstname == y.firstname && x.lastname == y.lastname; } public int gethashcode(person obj) { unchecked { var hash = 17; hash = hash * 23 + obj.firstname.gethashcode(); hash = hash * 23 + obj.lastname.gethashcode(); return hash; } } }
implementation
static void main() { var lista = person.createlist(); var listb = person.createlisttwo(); //attempt var result = lista.join( listb, x => x, y => y, (x, y) => new person { firstname = x.firstname, lastname = x.lastname, updateme = y.updateme }, new firstlastnamematch() ); foreach (var person in result) { console.writeline($"name: {person.firstname} {person.lastname} " + $"updateme: {person.updateme} "); } }
there problem run here is inner join , not left join. have found way left join cannot figure out how pass in iequalitycomparer
syntax.
var result = persona in lista join personb in listb on new {persona.firstname, persona.lastname } equals new { personb.firstname, personb.lastname } buffer subperson in buffer.defaultifempty() select new person { firstname = persona.firstname, lastname = persona.lastname, updateme = (subperson == null ? string.empty : subperson.updateme) };
the real catch once able complete left join while injecting comparison criteria, still need detect duplicates. need able identify if person lista
matches more 1 person in listb
. alright if lista
had duplicates though.
end goal
left join 2 lists dynamic matching criteria.
detect multiple matches right list.
need update static list of properties on left list match right list.
Comments
Post a Comment