-1

Here is my previous post.

https://stackoverflow.com/questions/75106799/how-to-get-data-from-3-table-into-1-list/.

It works fine. But i expect returning value as:

[{ "id": "1f5a6c7c-6168-4ac8-73a5-08daf474a373", "name": "Bag A", "quantity": 10, "category": "Cat-A" }, { "id": "9b8eb0cc-0da4-4b6a-73a6-08daf474a373", "name": "Shirt A", "quantity": 10, "category": "Cat-B" }, { "id": "DB2EE420-A4E5-407A-5F96-08DAF4759F9C", "name": "Shoes A", "quantity": 10, "category": "Cat-C" } ]

I use .net core 6 MVC - code first. Please help me.

Md Farid Uddin Kiron
  • 16,817
  • 3
  • 17
  • 43
Nguen
  • 27
  • 4

1 Answers1

2

I want to return value without "bag", "shirts","shoes". Look like above:

Well, based on your question, you could achieve by introduing new model which would be containing all property that your "bag", "shirts","shoes" containing. Finally, you need to loop through the existing list item and bind those into new class/model. You can have a try in following way:

Create A new Model for generic List:

public class GenericClass
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Quantity { get; set; }
        public string Category { get; set; }
    }

Note: We will use this model to loop over our existing list thus we would bind them into this.

Base Model:

public class GenericClass
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Quantity { get; set; }
        public string Category { get; set; }
    }
    public class Bags
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Quantity { get; set; }
        public string Category { get; set; }
    }
    public class Shirts
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Quantity { get; set; }
        public string Category { get; set; }
    }
    public class Shoes
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Quantity { get; set; }
        public string Category { get; set; }
    }

Seed Data Into Model:

List<Bags> listBags = new List<Bags>();
            listBags.Add(new Bags() { Id = 101, Name = "Bag A", Quantity = 10, Category = "Cat-A" });
            listBags.Add(new Bags() { Id = 102, Name = "Bag B", Quantity = 15, Category = "Cat-A" });
            listBags.Add(new Bags() { Id = 103, Name = "Bag C", Quantity = 20, Category = "Cat-A" });

            List<Shirts> listShirts = new List<Shirts>();
            listShirts.Add(new Shirts() { Id = 101, Name = "Shirt A", Quantity = 10, Category = "Cat-B" });
            listShirts.Add(new Shirts() { Id = 102, Name = "Shirt B", Quantity = 15, Category = "Cat-B" });
            listShirts.Add(new Shirts() { Id = 103, Name = "Shirt C", Quantity = 20, Category = "Cat-B" });

            List<Shoes> listShoes = new List<Shoes>();
            listShoes.Add(new Shoes() { Id = 101, Name = "Shirt A", Quantity = 10, Category = "Cat-S" });
            listShoes.Add(new Shoes() { Id = 102, Name = "Shirt B", Quantity = 15, Category = "Cat-S" });
            listShoes.Add(new Shoes() { Id = 103, Name = "Shirt C", Quantity = 20, Category = "Cat-S" });

Build Custom List:

var genericClass = new List<GenericClass>();
           
            foreach (var item in listBags)
            {
                var bag = new GenericClass();
                bag.Id = item.Id;
                bag.Name = item.Name;
                bag.Quantity = item.Quantity;
                bag.Category = item.Category;
                genericClass.Add(bag);
            }

            foreach (var item in listShirts)
            {
                var shirt = new GenericClass();
                shirt.Id = item.Id;
                shirt.Name = item.Name;
                shirt.Quantity = item.Quantity;
                shirt.Category = item.Category;
                genericClass.Add(shirt);
            }

            foreach (var item in listShoes)
            {
                var shoes = new GenericClass();
                shoes.Id = item.Id;
                shoes.Name = item.Name;
                shoes.Quantity = item.Quantity;
                shoes.Category = item.Category;
                genericClass.Add(shoes);
            }

Complete Demo:

        [HttpGet("GetFrom3TablesWithSameKey")]
        public IActionResult GetFrom3TablesWithSameKey()
        {
            List<Bags> listBags = new List<Bags>();
            listBags.Add(new Bags() { Id = 101, Name = "Bag A", Quantity = 10, Category = "Cat-A" });
            listBags.Add(new Bags() { Id = 102, Name = "Bag B", Quantity = 15, Category = "Cat-A" });
            listBags.Add(new Bags() { Id = 103, Name = "Bag C", Quantity = 20, Category = "Cat-A" });

            List<Shirts> listShirts = new List<Shirts>();
            listShirts.Add(new Shirts() { Id = 101, Name = "Shirt A", Quantity = 10, Category = "Cat-B" });
            listShirts.Add(new Shirts() { Id = 102, Name = "Shirt B", Quantity = 15, Category = "Cat-B" });
            listShirts.Add(new Shirts() { Id = 103, Name = "Shirt C", Quantity = 20, Category = "Cat-B" });

            List<Shoes> listShoes = new List<Shoes>();
            listShoes.Add(new Shoes() { Id = 101, Name = "Shirt A", Quantity = 10, Category = "Cat-S" });
            listShoes.Add(new Shoes() { Id = 102, Name = "Shirt B", Quantity = 15, Category = "Cat-S" });
            listShoes.Add(new Shoes() { Id = 103, Name = "Shirt C", Quantity = 20, Category = "Cat-S" });


            var genericClass = new List<GenericClass>();
           
            foreach (var item in listBags)
            {
                var bag = new GenericClass();
                bag.Id = item.Id;
                bag.Name = item.Name;
                bag.Quantity = item.Quantity;
                bag.Category = item.Category;
                genericClass.Add(bag);
            }

            foreach (var item in listShirts)
            {
                var shirt = new GenericClass();
                shirt.Id = item.Id;
                shirt.Name = item.Name;
                shirt.Quantity = item.Quantity;
                shirt.Category = item.Category;
                genericClass.Add(shirt);
            }

            foreach (var item in listShoes)
            {
                var shoes = new GenericClass();
                shoes.Id = item.Id;
                shoes.Name = item.Name;
                shoes.Quantity = item.Quantity;
                shoes.Category = item.Category;
                genericClass.Add(shoes);
            }

            return Ok(genericClass);
        }

Output:

enter image description here

enter image description here

Note: If you still have any concern please have a look on our official documnet here.

Md Farid Uddin Kiron
  • 16,817
  • 3
  • 17
  • 43
  • Alright, but while posting new question, share more details and your current code snippet where you are stuck in which would make contributor to visualize your issue accordingly. – Md Farid Uddin Kiron Jan 16 '23 at 07:04