c# - Convert or map a list of class to another list of class by using Lambda or LINQ? -
How to change the list of MyData in the second list of MyData2? For example:
list & lt; MyData & gt; List1 = New list & lt; MyData & gt; (); // Somewhere 1 is a Populated List & lt; MyData2 & gt; List2; // Now I need list2 from list 2 to get List2 = ... how to use similar LINQ or Lambda?
Here I tried it but I could not detect the code:
list2 = (List of X 1 where list1.PropertyList = = tap new select MyData2 (null, x.PropertyB, x.PropertyC) Union (1 lists the list of top list. property list! = null new MyData2 (/ * * how to Propianist * / y.PropertyB, y. how to)) Kolist (each item in PropertyC);
While Maidata 2 is a CTR (string, string, string).
If there are two different types, you'll use the same list to map to the new list.
list2 = list1.Select (x = & gt; Add new MyData2 () {// your variable mapping PropertyB = x.PropertyB, PropertyC = x.PropertyC}) Kolist ();
edit :
now that you've changed your question you are that you can do something like that to happen to you.
list2 = list1 .Aggregate (new list & lt; MyData2 & gt; (), (X, y) = & gt; {If (Y.PropertyList == null) x.Add (New MyData2 (tap, y.PropertyB, y.PropertyC)); And x.AddRange (y.PropertyList.Select (z = & new; MyData2 (z, y.PropertyB, Y.PropertyC))); Return x;});
Comments
Post a Comment