I have a list. Based on some properties of object of type A, I want to merge one or more object of type A from the list and form a new object of type B. Merging will be based on some predefined rules.
something like automapper, but input is a list of object.
Example
class A {
Guid source;
int ref;
string text1;
string text2;
int Value
}
class B {
List<Guid> source;
int ref;
List<string> text;
int Value
}
for all the elements of a list which has the same ref, I want to merge based on rules like
if source has values in (source1, source2, source3)
then B.text = a1.text1 +a2.text1 +a3.text1
and b.Value = a1.Value +a2.Value +a3.Value
but if source has value in (source4)
then it is a simple mapping of values.
This there a simple and efficient way of configuring this kind of merging rules.
Thanks in advance.
let say List has 100 elements, with 30 different ref values and for reach ref values there are more than one source values. so the final count of elements in List will be less than 100.
You can use
GroupByon multi-column (refandsourcein this case) in LINQ query to perform desired transformation. The tricky part is use a conditional grouping on 2nd column.Since you have a special
source(e.g. source4)for which grouping is not desired. Hence, a dummy column forGroupBycan be added that will have fixed value ifsourceis notsource4but anew unique (new Guid())if itssource4.The classes and sample data used in above
code-snippetare: