-2

Sorry for my bad English.

Here is my SQL Design.

SqlDesign

I have 3 table in Sqlsever. Each table has 4 column with same name, same datatype.

And i want to get data from 4 column "Id, Name, Quantity, IdCategory" from 3 table into 1 list object same as returning value in this code below:

public async Task<IEnumerable<Shirt>> LoadAllShirt()
{
    return await _dbContext.Shirt.ToListAsync();
}

I use .NET Core 6 Mvc - code first. Thanks for your help.

Md Farid Uddin Kiron
  • 16,817
  • 3
  • 17
  • 43
Nguen
  • 27
  • 4
  • 1
    Ok, but what is your question? What are you expecting of us? Are you expecting us to write the whole code for you? That's not how stack overflow works. Have you looked at the [Entity Framework documentation](https://learn.microsoft.com/en-us/ef/) by Microsoft? Or other EF Core resources like [Entity Framework Tutorial](https://www.entityframeworktutorial.net/efcore/entity-framework-core.aspx)? – JHBonarius Jan 13 '23 at 08:50
  • 1
    It seems that you are looking for `UNION ALL` Sql or `Concat` EF: https://stackoverflow.com/questions/9828308/how-can-i-do-a-union-all-in-entity-framework-linq-to-entities – Dmitry Bychenko Jan 13 '23 at 08:51
  • I just want to know is that possible. And how i can get them. – Nguen Jan 13 '23 at 08:51
  • Yes, it's possible. It's called projection. You can get them by writing code according to the documentation. But your request is weird, because your code shows you want to cast a e.g. a "Bag" to a "Shirt". – JHBonarius Jan 13 '23 at 08:54
  • Thanks for your reply. I just want to get all item in 3 table. – Nguen Jan 13 '23 at 08:59
  • I also want a lot of stuff. However, I have to work for it. Same for you: stack overflow is not a free coding service. We can help you with coding issues. But we're not going to write the whole code for you. There are many examples around and good tutorials. Please put in some effort yourself. – JHBonarius Jan 13 '23 at 09:27

3 Answers3

1

I have 3 table in Sqlsever. Each table has 4 column with same name, same datatype. And I want to get data from 4 column "Id, Name, Quantity, IdCategory" from 3 table into 1 list, I use .NET Core 6 Mvc - code first.

Well, lot of way around to handle this kind of scenario. Most easy and convenient way I would prefer to use View model or using Linq query.

Lets assume you have below Models:

Models:

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; }
    }

Seeds In Models:

            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" });

Way: 1 using ViewModel:

public class AllViewModel
    {
        public List<Bags> Bags { get; set; }
        public List<Shirts> Shirts { get; set; }
        public List<Shoes> Shoes { get; set; }
    }

Query Using ViewModel:

            var allTableUsingViewModel = new AllViewModel();
            allTableUsingViewModel.Bags = listBags;
            allTableUsingViewModel.Shirts = listShirts;
            allTableUsingViewModel.Shoes = listShoes;

Output Using ViewModel:

enter image description here

enter image description here

Way: 2 using Linq Annonymous Type:

Query Using Linq Annonymous Type:

var AllTableListUsingLinq = from a in listBags
                             join b in listShirts on a.Id equals b.Id
                             join c in listShoes on b.Id equals c.Id
                             select new
                             {
                                 FromBagsID = a.Id,
                                 FromBagsName = a.Name,
                                 FromBagsQuantity = a.Quantity,
                                 FromBagsCategory = a.Category,

                                 FromShirtsID = b.Id,
                                 FromShirtsName = b.Name,
                                 FromShirtsQuantity = b.Quantity,
                                 FromShirtsCategory = b.Category,

                                 FromShoesID = c.Id,
                                 FromShoesName = c.Name,
                                 FromShoesQuantity = c.Quantity,
                                 FromShoesCategory = c.Category

                             };

Output Using Linq Annonymous Type:

enter image description here

Full Controller:

        [HttpGet("GetFrom3Tables")]
        public IActionResult GetFrom3Tables()
        {
            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" });

            //Way: 1 Linq Query

            var AllTableListUsingLinq = from a in listBags
                             join b in listShirts on a.Id equals b.Id
                             join c in listShoes on b.Id equals c.Id
                             select new
                             {
                                 FromBagsID = a.Id,
                                 FromBagsName = a.Name,
                                 FromBagsQuantity = a.Quantity,
                                 FromBagsCategory = a.Category,

                                 FromShirtsID = b.Id,
                                 FromShirtsName = b.Name,
                                 FromShirtsQuantity = b.Quantity,
                                 FromShirtsCategory = b.Category,

                                 FromShoesID = c.Id,
                                 FromShoesName = c.Name,
                                 FromShoesQuantity = c.Quantity,
                                 FromShoesCategory = c.Category

                             };

            //Way: 2 : ViewModel
            var allTableUsingViewModel = new AllViewModel();
            allTableUsingViewModel.Bags = listBags;
            allTableUsingViewModel.Shirts = listShirts;
            allTableUsingViewModel.Shoes = listShoes;
           

            return Ok(AllTableListUsingLinq);
        }

Note: If you need more information you could check our official document for View Model and Linq Projction here

Md Farid Uddin Kiron
  • 16,817
  • 3
  • 17
  • 43
  • Hello sir. I use ViewModel and it's work. But i want to return value without "bag", "shirts","shoes". Look like: [{ "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" } ]. Please help me with that issue – Nguen Jan 16 '23 at 03:21
  • Thanks for your response Nguen, it would be great if you post new question so that our this question wouldn't lost its uniqueness as stackoverflow allows single answer within a question. – Md Farid Uddin Kiron Jan 16 '23 at 03:28
  • https://stackoverflow.com/questions/75130044/how-to-get-data-from-3-table-into-1-list-json Here is my new post sir. – Nguen Jan 16 '23 at 03:39
  • Okay, you could have a try now. – Md Farid Uddin Kiron Jan 16 '23 at 06:46
0

The following sample query will list your 3 types of data into a single result set.

var allResults = resultSet1.Concat(resultSet2);

For the return type create a class which will be the parent class for all your products (Bag,Shirt,Shoes) Which will help you to return data in a single Generic data.

If you use any non-generic list to send the data like hashtable or Arraylist then then there will be no issue.

In my way I will suggest to use generic data list as it will help you fetch data in better time complexity.

0

In this case you may need to define additional indirect base class with these 4 parameters. Than you can create Collection of this base class, and concatinate all 3 tables into.

public class BaseEntity
{
    public string Name {get;set;}
}

public class Shoes : BaseEntity
{

}
...
public IEnumerable<BaseEntity> GetAllTables()
{
    var shirts = await _dbContext.Shirt.ToListAsync();
    var shoes = await _dbContext.Shoes.ToListAsync();
    var bags = await _dbContext.Bags.ToListAsync();

    return shirts.Concat(shoes).Concat(bags);
}

Similar example but witout casting to base class is shown in Enumerable.Concat documentation: https://learn.microsoft.com/pl-pl/dotnet/api/system.linq.enumerable.concat?view=net-7.0

  • Will not work unless you cast to the base type. Also, it's not very efficient to do this in memory. You can do this in the server if you use projection. – JHBonarius Jan 13 '23 at 09:25