Search code examples
c#dictionarylinq-to-sql

Query multiple tables and send query to Dictionary


I'm trying to query multiple table and save the query as a global dictionary for further processing. I've tried the following, but instead of values I get the class name in the dictionary. Please take a look and show me what's wrong and where to read up more on todictionary queries?

  public class linqtosql
{
    public Dictionary<int, MC_VARIABLES> dctMC = new Dictionary<int, MC_VARIABLES>();

    public class MC_VARIABLES
    {
        public int ID { get; set; }
        public int UDLY_LAST { get; set; }
        public int STRIKE { get; set; }
        public decimal SKEW_A { get; set; }
        public decimal SKEW_B { get; set; }
        public double SKEW_C { get; set; }
    }

    public void GET_DATA()
    {

        var qryBOOK = from B in Globals.DATA.BOOKs
                      from O in Globals.DATA.OPTIONs
                      from U in Globals.DATA.UDLies
                      from S in Globals.DATA.SKEWs
                      where B.CONTRACT == O.CONTRACT
                      where O.UDLY_SYMBOL == U.UDLY_SYMBOL
                      where O.CONTRACT == S.CONTRACT
                      select new MC_VARIABLES
                      { ID = B.ID, STRIKE = (int)B.STRIKE, SKEW_A = (decimal)S.SKEW_A };

        dctMC = qryBOOK.ToDictionary(x => x.ID, x => x);

        foreach (KeyValuePair<int, MC_VARIABLES> KVP in dctMC)
        {
            var key = KVP.Key;
            var item = KVP.Value.SKEW_A;
        }
    }
}

Solution

  • it should be x => x instead of x => MC_VARIABLES, x is of type MC_VARIABLES in this case.

    qryBOOK.ToDictionary(x => x.ID, x => x)