c# - How to merge two lists based on a property? -
i have 2 lists, 1 fake , 1 real, like:
before
// fake (list 1) { id = 1, year = 2011, x = "" } , { id = 2, year = 2012, x = "" } , { id = 3, year = 2013, x = "" } // real (list 2) { id = 35, year = 2011, x = "information" } , { id = 77, year = 2013, x = "important" }
i want merge them looking year, result should be:
after
{ id = 35, year = 2011, x = "information" } , { id = 2, year = 2012, x = "" } , { id = 77, year = 2013, x = "important" }
it must remove elements same year on first list , add element equivalent year on second list first list, keeping order.
how can using linq?
you should able using "left join":
from f in fake join r in real on f.year equals r.year joinresult r in joinresult.defaultifempty() select new { id = r == null ? f.id : r.id, year = f.year, x = r == null ? f.x : r.x };
Comments
Post a Comment