let's assume I have a list which has elements with missing values but matching id(which may or not may be missing so I would like to take the values which arent null or empty and put them into a single element which in the matching field name converts to a IGrouping/list if there are several values which arent null or a single null value if there none,
this is an example
public class MyClass
{
public int vali;
public string vala;
public string valb;
public long? vall;
}
var list = new List<MyClass>()
{
new MyClass(){vali= 2,vala=null,valb="how are you",vall=7},
new MyClass {vali=3,vala="hi",valb="how are you doing",vall=null},
new MyClass{vali=2,vala="hello",valb="how are you",vall=null},
new MyClass{vali=3,vala=null,valb=null,vall=8},
new MyClass(){vali= 2,vala=null,valb=null,vall=7},
};
I would like to get the following output
{2,"hello",string[] {"how are you","how are you" },int[] {7,7} }
{3 , "hi" , "how are you doing" ,8 },
using linq
list.GroupBy(x=>x.vali).Select(x=>x.FirstOrDefault()).ToList();
returns
{2,"how are you",null,7},
{3, "how are you doing",null}
so what query could I use or how could I implement to return a list(or a IGrouping as linq does but writing my own algorithm)
Thank you