如何在返回对象属性

时间:2016-09-05 10:38:36

标签: c# linq ssis

我想写一个脚本来发现每个航空公司哪个班级是First,Business等等。

我要做的第一件事是从数据库表中提取匹配,如下所示:

Sample table values here

public HashSet<CategoriaTariffePerCompagnia> _classe;
public ICollection<CategoriaTariffePerCompagnia> ElencoClassi
{
    get
    {
        //Explicit Lazy-pattern
        if (_classe == null)
        {
            _classe = new HashSet<CategoriaTariffePerCompagnia>();
            using (var cnn = (SqlConnection)Connections.Reporting.AcquireConnection(null))
            using (var cmd = cnn.CreateCommand())
            {
                //Set the query here

                //only load from the db what you really need
                cmd.CommandText = "SELECT ID_Compagnia, ID_Classe, Classe FROM [dbo].[DIM_ClassiCompagnie]";
                cmd.CommandType = CommandType.Text;
                //start the query. it actually opens a server side cursor on sql server
                var dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    _classe.Add(new CategoriaTariffePerCompagnia()
                    {
                        ID_Compagnia = dr["ID_Compagnia"].ToString(),
                        ID_Classe = dr["ID_Classe"].ToString(),
                        Classe = dr["Classe"].ToString()
                    });
                }

            }
        }
        return _classe;
    }
}

然后在脚本的主要部分中,我调用以下方法:

public override void Input0_ProcessInputRow(Input0Buffer Row)
{
    var classi = ElencoClassi;
    Row.Classe = TrovaClasse(Row.CodiceCompagnia, Row.ClasseVettore, classi);        
}

static string TrovaClasse(string ID_Compagnia, string ID_Classe, IEnumerable<CategoriaTariffePerCompagnia> classi)
{
    var q = (from c in classi
             where c.ID_Classe.Equals(ID_Classe) && c.ID_Compagnia.Equals(ID_Compagnia)
             select c).FirstOrDefault<CategoriaTariffePerCompagnia>();

    if (q != null)
    {
        return q.Classe.ToString();
    }
    else
    {
        return "ND";
    }
}

Row.CodiceCompagnia是一个字符串,类似于&#34; 220&#34;和Row.ClasseVettore是一个包含类&#39;像#&#34; J&#34;

然而,出于某种原因,我每次尝试都会获得ND。所以我开始相信查询中的某些内容必定是错误的,否则var q应该等于像

这样的对象

{ID_Compagnia =&#34; 220&#34;,ID_Classe =&#34; J&#34;,Classe =&#34; Business&#34; }

并且不应该为空。

有什么想法吗?

/编辑:

我尝试在控制台程序上运行这样的东西:

static void Main(string[] args)
    {

        ColonneInput Input = new ColonneInput { ID_Comp = "220", ID_Class = "X" };
        CategoriaTariffePerCompagnia ec = new CategoriaTariffePerCompagnia { ID_Compagnia = "220", ID_Classe = "X", Classe = "Business" };

        var InputColonne = new List<ColonneInput>() { Input };
        var ElencoClassi = new List<CategoriaTariffePerCompagnia>() { ec };
        //LINQ Query
        var q = (from c in ElencoClassi
                from i in InputColonne
                where (c.ID_Classe == i.ID_Class) && (c.ID_Compagnia == i.ID_Comp)
                select c.Classe).FirstOrDefault();

        if (q == null)
        {
            q = "Sconosciuta";
        }




        Console.WriteLine( q.ToString() );
        Console.WriteLine("Press ESC to stop");
        do
        {
            while (!Console.KeyAvailable)
            {
                // Do something
            }
        } while (Console.ReadKey(true).Key != ConsoleKey.Escape);
    }

它有效!所以现在看来​​,大型物体的创造存在某种问题&#34; ElencoClassi&#34; ......

0 个答案:

没有答案