C#linq to SQL todictionary [英] C# linq to SQL todictionary

查看:209
本文介绍了C#linq to SQL todictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用linq查询几个表到sql但无法正确获取代码 - 继续获取强制转换或匿名类型错误。请任何人都可以提供一个好的示例代码,书籍或教程来增加我的知识吗?



我尝试过:



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:

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

推荐答案

首先,你需要修复你的声明:

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



然后,你不如果在被引用之前始终在 GET_DATA 中初始化,则需要在此初始化它。这里创建的空字典将通过 GET_DATA


中的分配放在地板上只是谷歌c#todictionary而你找到用法示例。你需要提供两个参数,一个让你知道密钥是什么,一个是价值;



http://www.dotnetperls.com/todictionary [ ^ ]
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[^]


这是最终的解决方案。



here is a final solution.

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


这篇关于C#linq to SQL todictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆