VB Linq to C#covertion error“名称'Model'在当前上下文中不存在”

时间:2015-09-23 21:25:44

标签: c# vb.net linq

我有一个程序,我正在从VB转换为C#,并且在Linq语句中出现“当前上下文中不存在名称'Model'的错误”。 “Model”列是“rdfcounts”表中的行标题,因此它确实存在。如何在select语句中调用实际列?

var ACFTModel = (
    from rdfcounts in dc.RDFCounts
    where (Convert.ToString(rdfcounts.RDate.Value.Year) + Convert.ToString(rdfcounts.RDate.Value.Month)) == (RDFDate1.ToString())
    group rdfcounts by rdfcounts.Model into g
          select new { MonRDF = (RDFMDate.ToString() + " " + Model = g.Key + "" + Convert.ToString(g.Sum((p) => p.RDFNUm))) }).ToArray();

这是Linq VB代码,它确实有效。

   Dim ACFTModel = (From rdfcounts In dc.RDFCounts _
                Where (CStr(rdfcounts.RDate.Value.Year) _
                + CStr(rdfcounts.RDate.Value.Month)) = (RDFDate1.ToString) _
                        Group rdfcounts By rdfcounts.Model Into g = Group _
                        Select _
                        MonRDF = (RDFMDate.ToString + " " + Model + "" _
                        + CStr(g.Sum(Function(p) p.RDFNUm))) _
                        ).ToArray

2 个答案:

答案 0 :(得分:0)

所以,问题是:

group rdfcounts by rdfcounts.Model into g

您要将Model列分组到论坛g

这为您提供了一个分组集合,每个组的密钥都设置为值rdfcounts.Model。从本质上讲,这意味着Model现在位于您选择的g.Key下。

看起来您几乎已经完成了这项工作,除非您尝试进一步过滤Model = g.Key(注意C#等式是==,而不是=) 。这保证是正确的,因为您只是按模型分组(假设Model仍可访问)。

长话短说,将Model = g.Key替换为g.Key

答案 1 :(得分:0)

我能够更改运行代码的先前阵列上的数据类型。不是它的工作正常。

            RDFCountDataContext dc = new RDFCountDataContext();
        object[] ModelArray = null;
        int aptID = 0;
        aptID = 0;

        Array RDFMDate = ((
            from rdfcounts in dc.RDFCounts
            select new {Expr1 = (Convert.ToString(rdfcounts.RDate.Value.Year) + Convert.ToString(rdfcounts.RDate.Value.Month))}).Distinct()).ToArray();

// INSTANT C#注意:由于在foreach'中循环变量,因此对此声明进行了评论。循环在'foreach'中声明。 C#中的标题:

        foreach (String RDFDate1 in RDFMDate)
        {

            MAppointment apt1 = new MAppointment();
            //var strOriginal = RDFDate1.Substring(4) + "/" + RDFDate1.Substring(4) + "/" + RDFDate1.Substring(0, 4);
            var strOriginal = RDFDate1.Substring(4) + "/" + RDFDate1.Substring(4) + "/" + RDFDate1.Substring(0, 4);
            DateTime dt = DateTime.Parse(strOriginal);

            apt1.StartTime = dt;

            var ACFTModel = (
                from rdfcounts in dc.RDFCounts
                where (Convert.ToString(rdfcounts.RDate.Value.Year) + Convert.ToString(rdfcounts.RDate.Value.Month)) == (RDFDate1.ToString())
                group rdfcounts by rdfcounts.Model into g
                //select new { MonRDF = (RDFMDate.ToString() + " " + Model = g.Key + "" + Convert.ToString(g.Sum((p) => p.RDFNUm))) }).ToArray();
                select new { MonRDF = (RDFMDate.ToString() + " " + g.Key + "" + Convert.ToString(g.Sum((p) => p.RDFNUm))) }).ToArray();


            ModelArray = ACFTModel;