Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to query several tables using linq to sql but can't get the code right - keep getting casting or anonymous type errors. Please can anyone suggest a good sample code, book or tutorial to increase my knowledge?

What I have tried:

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

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

        //MC_VARIABLES myThing = dctMC[3];
        //var last = myThing.STRIKE;

        foreach (var item in dctMC)
        {
            MC_VARIABLES myThing = dctMC[item];   !problem here
            var last = myThing.STRIKE;
        }
    }
}
Posted
Updated 15-Mar-16 6:44am
v7
Comments
Wonde Tadesse 14-Mar-16 22:32pm    
The error is not related to toDictonary method. That is perfect. The issue is that there is a cast error around (int)B.STRIKE, SKEW_A = (decimal)S.SKEW_A. Make sure these two variables (B.STRIKE and S.SKEW_A) are cast-able to int and decimal respectively.

First of all, you need to fix your declaration:
C#
public Dictionary<int, MC_VARIABLES> dctMC = new Dictionary<int, MC_VARIABLES>();

Then, you don't need to initialize it here if it always will be initialized in GET_DATA before being referenced. The empty Dictionary created here will be "dropped on the floor" by the assignment in GET_DATA
 
Share this answer
 
Comments
Sascha Lefèvre 14-Mar-16 18:02pm    
+5Additional suggestion for Trader999:
I have a hunch it would make sense making the class linqtosql static, the method GET_DATA static, removing the class-member dctMC and returning qryBOOK.ToDictionary(x => x.ID) from GET_DATA (so changing it from being void to Dictionary<int, MC_VARIABLES>). But as I don't know your intention you have to judge this suggestion.
Matt T Heffron 14-Mar-16 19:09pm    
Thanks.
I think I'd opt for making the data a parameter to GET_DATA instead of directly referencing Globals.DATA! (yeech!)
Sascha Lefèvre 14-Mar-16 19:18pm    
Yep :)
Just google "c# todictionary" and you'll find usage examples. You need to supply two params, one that lets you know what the key is and one for the value;

http://www.dotnetperls.com/todictionary[^]
 
Share this answer
 
Comments
Sascha Lefèvre 14-Mar-16 17:57pm    
Not correct, there's a .ToDictionary(..) overload that takes just a KeySelector. The problem here is the missing generic type declaration for the dictionary.
Trader999 14-Mar-16 19:27pm    
Thanks for all the input. Matt I have fixed the dictionary declaration, but get funny data in the dictionary (see below) and not the values. I think it has to do with the second parameter in the todictionary line, but I'm not sure how to fix it.

[0] = {[3, nmMONTECARLO.linqtosql+MC_VARIABLES]}

I've tried
dctMC = qryBOOK.ToDictionary(x => x.ID,x=>MC_VARIABLES);
but I'm just hacking and hoping something sticks.

The idea is to query a bunch of tables, save the query in a global dictionary (or class property) and then use the values in the dictionary further in the code.

I've had a look at many many examples on Google, but nothing seems to fit my needs.

Matt I could put the datacontext (Globals.DATA) in the fn GET_DATA, but I will use the datacontext elsewhere in the program and opted for a global datacontext since there will only be on user and getting and saving data will be done sequentially so there shouldn't be a conflict (assuming I understand this correctly!)

Sascha I think you are right to return the query dictionary, this was the next step once I get this working. What is best, return the dictionary as a global variable or save it as a class property?
Matt T Heffron 14-Mar-16 20:56pm    
Normally, I would only see be notified of a reply to MY Solution.
I saw this only by "wandering by..."
If you repost some of these comments to my Solution, I can answer them "in context"...
Trader999 14-Mar-16 22:09pm    
got the code to work with some other help, but how can I loop through the dictionary? (see edit)
Matt T Heffron 15-Mar-16 12:42pm    
Just another suggestion about using Code Project:
Please don't replace a comment with a new one. (minor tweaks are OK...)
Just add a new comment.
That way the whole history of the discussion remains for everything to be read in context.
here is a final solution.

C#
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;
           }
       }
   }
 
Share this answer
 
Comments
Matt T Heffron 15-Mar-16 12:26pm    
FYI: It is generally considered an abuse of the Reputation Points system to post your own Solution unless you solved your problem independent of other assistance.
The preferred way to show the final resolution to your question would be to use the "Improve question" and add the resolution below the original question. That way in the future when someone may find this question in a search they can see the whole "history".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900