locked
How to navigate based on the result of one table to another table? RRS feed

  • Question

  • I have got a Table let says T1. This table has two columns one is for the value and for referencing to another Table. Let say Referencing table are Ta, Tb, Tc.  

    For example from the value 1.44, I get the name of Tb. Up to this point we got the reference to table Tb, now how can I query the Tb?

    I got no clue what should be the structure of T1.

    What I am currently doing is this:

    using (var ctx = new DNVDBEntities1()) { var tableName1 = (from value in ctx.ListOfTables where value.Values <= (decimal)tMinuscByDo orderby value.Values descending select value.DNVTables).FirstOrDefault(); var tableName2 = (from value in ctx.ListOfTables where value.Values >= (decimal)tMinuscByDo orderby value.Values ascending select value.DNVTables).FirstOrDefault(); double K1 = ReturnK(tableName1.Trim(), ctx); double K2 = ReturnK(tableName2.Trim(), ctx); K = (K1 + K2) / 2; }


            private double ReturnK(string TableName, DNVDBEntities1 ctx)
            {
                double K1=-1;
                if (TableName == "tMinusCbyD0dot002")
                    K1 = (double)(from tMinusC in ctx.tMinusCbyD0dot002
                                  where tMinusC.HbyD <= (decimal)hByDo
                                  orderby tMinusC.HbyD descending
                                  select tMinusC.K).FirstOrDefault();
                else if(TableName == "tMinusCbyD0dot005")
                    K1 = (double)(from tMinusC in ctx.tMinusCbyD0dot005
                                  where tMinusC.HbyD <= (decimal)hByDo
                                  orderby tMinusC.HbyD descending
                                  select tMinusC.K).FirstOrDefault();
               else if (TableName == "tMinusCbyD0dot01")
                    K1 = (double)(from tMinusC in ctx.tMinusCbyD0dot002
                                  where tMinusC.HbyD <= (decimal)hByDo
                                  orderby tMinusC.HbyD descending
                                  select tMinusC.K).FirstOrDefault();
                else if (TableName == "tMinusCbyD0dot02")
                    K1 = (double)(from tMinusC in ctx.tMinusCbyD0dot005
                                  where tMinusC.HbyD <= (decimal)hByDo
                                  orderby tMinusC.HbyD descending
                                  select tMinusC.K).FirstOrDefault();
                else if (TableName == "tMinusCbyD0dot04")
                    K1 = (double)(from tMinusC in ctx.tMinusCbyD0dot005
                                  where tMinusC.HbyD <= (decimal)hByDo
                                  orderby tMinusC.HbyD descending
                                  select tMinusC.K).FirstOrDefault();
                return K1;
            }

    Entity framework 6 - C#



    Thursday, December 25, 2014 11:30 AM

Answers

  • Hello Bopy,

    Since you could get the table name and you just want to return one column value, you could use Raw Query instead:

    using (DFDBEntities db = new DFDBEntities())
    
                    {
    
                        SqlParameter latParam = new SqlParameter("@OrderDetailID", 1);
    
                        object[] parameters = new object[] { latParam };
    
                        var result = db.Database.SqlQuery<decimal>(string.Format("select od.Count from {0} od where od.OrderDetailID = @OrderDetailID ", "OrderDetail"), parameters).FirstOrDefault();
    
                    }
    

    The LINQ query could not do such dynamic query with the table name.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, December 26, 2014 2:50 AM