locked
How to avoid multiple db calls ?? RRS feed

  • Question

  • User527076549 posted

    Hi

    I have requirement, where based on Id i need to get values  So  i am using switch satement

    s there any way to optimize it ?  so below is sample like.

    Switch(condition)

    case conditon1:  value = dbcall.table.select();

    case condiion2:  value =dbcall.table2.select()

    etc

    Monday, June 1, 2020 5:21 AM

Answers

  • User-719153870 posted

    Hi amithashenoy,

    Thank you for the code shared. But we cannot see multiple db calls from the code. If you mean the _context before each table, i think that is necessary.

    In addition, since the query is in the switch statement, it means there will be only one db call at a time.

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 2, 2020 1:50 AM

All replies

  • User379720387 posted

    One db call is what you will have with a Switch.

    Monday, June 1, 2020 8:44 AM
  • User-719153870 posted

    Hi amithashenoy,

    The description is not that clear, how do you interact with the database? ADO.NET? EF?

    If you just want to change the target table to query when the conditions are different, then there will be no need to connect to your db multiple times.

    Connect to db then change the query filters in switch statement.

    Would you please share more detailed code so that we can understand current requirement?

    Thanks for your understanding.

    Best Regard,

    Yang Shen

    Monday, June 1, 2020 8:46 AM
  • User527076549 posted

    Yes, target table changes when condition changes , so  on every condition i am making db call ... below how code looks...Any suggestions And using EF with LINQ

    foreach (var items in table1)
    {
        foreach (var item in items.List)
        {
             switch (item.condition)
             {
                 case condition1:
                 case condition2:
                     item.nvalue= string.Join(",", _context.table1.Where(x => ids.Contains(x.Id)).Select(x => x.Title));
                     break;
    
                 case condition3:
                     item.nvalue= string.Join(",", _context.tabl2.Where(x => secondIds.Contains(x.Id)).Select(x => x.newvalu));
                     break;
    
                 case condition4:
                     item.nvalue= string.Join(",", _context.tabl3.Where(x => someIds.Contains(x.Id)).Select(x => x.oldvalue));
                     break;
    
                 case condition5:
                     item.nvalue= string.Join(",", _context.tabl4.Where(x=>textIds.Contains(x.Id)).Select(x => x.note));
                     break;
    
                 default:
                     item.nvalue= "";
                     break;
             }
         }
     }
    Monday, June 1, 2020 9:00 AM
  • User-719153870 posted

    Hi amithashenoy,

    Thank you for the code shared. But we cannot see multiple db calls from the code. If you mean the _context before each table, i think that is necessary.

    In addition, since the query is in the switch statement, it means there will be only one db call at a time.

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 2, 2020 1:50 AM