1

my db returns a list of object3 which i want to transform to object1 with list of items(object2) (has same same properties which are in object3)

  List<object3> test()
        {
            var objectx = new object3
            {
                property1 = "x1",
                property2 = "x2",
                property3 = "x3",
                property4 = "x4"
            };
            var objectxx = new object3
            {
                property1 = "x1",
                property2 = "x2",
                property3 = "x3",
                property4 = "x4"
            };

            return new List<object3>() { objectx, objectxx };
        }

    }
    class object1 {
            public string property1 { get; set; }
        public string property2 { get; set; }
        public List<object2> Items { get; set; }
}

    class object2
    {
        public string property3 { get; set; }
        public string property4 { get; set; }
    }
    class object3
    {
        public string property1 { get; set; }
        public string property2 { get; set; }
        public string property3 { get; set; }
        public string property4 { get; set; }
    }

My code which doesn't work, output is 2 records of object1, but i want 1 record of object1 with 2 items.

var x = test().Select(x => new object1
            {
                property1 = x.property1,
                property2 = x.property2,
                
                Items = new List<object2> {

                        new object2
                        {
                            property3 = x.property3,
                            property4 = x.property4
                        }
                }
            });
7
  • 2
    Please edit the question and include the classes instead of describing them. It's important to provide a minimal reproducible example in a debugging question. Moreover, "doesn't work" is not a good description of a problem. Do you get an error? Does it return a different result than you expect? Commented Jul 17, 2020 at 6:44
  • it seems that each entry of object3 will create only 1 item in your list public List<object2> items {get; set;} why do you need a list? Actually your code looks correct. But we don't know what you expect and what you actually get. You need to tells us this stuff Commented Jul 17, 2020 at 7:00
  • "my code which doesn't work" how do you test that it doesn't work? Commented Jul 17, 2020 at 7:00
  • i want to have only 1record of object3 with List of objects 2 Commented Jul 17, 2020 at 7:06
  • 1
    @SonDy The only problem that I can see with your code is that you used Items instead of items (I'm assuming that's just a typo in the question). Other than that, your code works just fine and output1 returns the same number of elements as the source list. Please try to provide a minimal reproducible example (i.e., one that can be copied and pasted) like I just did. Commented Jul 17, 2020 at 7:10

1 Answer 1

1

There's a tiny issue with the fact, that on the input side you have a flat structure with 4 properties, and on the output side you want a 2-tiered structure with 2 props on each layer. It just doesn't fit easily.

It basically means that you have to form things similar to 'object2' and then somehow mix&match them into groups called 'object1.items', at the same time, say, discarding duplicate property1+property2 pairs.

The code in your example is almost fine, but for each input item, it creates a new 'object1' item, and ends up with having a ton of 'object1' item, each having just a single 'object2'. There's no matching, there's no grouping.

Most probably, you want some kind of a GroupBy operation instead.

var output1 = _context.GetList<object3>(sql, parameters, CommandType.Text)
    .GroupBy(item => new{ item.property1, item.property2 })
    .Select(group =>
        new object1
        {
            property1 = group.Key.property1,
            property2 = group.Key.property2,
            items = group.Select(groupitem =>
                    new object2
                    {
                        property3 = groupitem.property3,
                        property4 = groupitem.property4,
                    }
                )
                .ToList()
        }
    )
    .ToList();

Interesting things:

  • note that the lambda in the GroupBy does not return object1 - it could, but it would complicate the code with IComparers/EqualsOverride/etc - instead its lambda returns 2 properties in an anonymous object, that way, GroupBy can easily compare their contents and form the groups
  • the 'Select' after GroupBy doesn't get items from the very source, but instead it gets groups formed by the GroupBy. A group is a list of items that had the same 'grouping key'=prop1+prop2, and that list also remembers that grouping key as .Key property. The key is what the GroupBy's lambda returned

small warning: code written from memory, never compiled, may contain small bugs and traces of milk and peanuts

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.