【问题标题】:Linq Anonymous type members must be declared in Sub QueryLinq 匿名类型成员必须在子查询中声明
【发布时间】:2015-03-30 19:07:43
【问题描述】:

我正在尝试将this SQL 查询转换为 Linq,以在 asp MVC 中的视图中返回,

Linq 中的预期输出:

TotalPoints Name

  231       John 

我在第二次选择新子查询时收到此错误:

无效的表达式术语'.'

匿名类型成员声明器无效。匿名类型成员必须 使用成员分配、简单名称或成员访问来声明。

到目前为止的 Linq 查询:

public ActionResult UserTotal()
        {
            List<TotalPointsUser> onetotal = from t in (
    (from LoyaltyDetailsTable in dbpoints.LoyaltyDetailsTable
    where
      LoyaltyDetailsTable.CustomerTable.CustomerId == 1
    group new {LoyaltyDetailsTable.CustomerTable, LoyaltyDetailsTable.LoayaltyPointsTable} by new {
      LoyaltyDetailsTable.CustomerTable.Name,
      LoyaltyPointsId = LoyaltyDetailsTable.LoayaltyPointsTable.LoyaltyPointsId,
      Points = LoyaltyDetailsTable.LoayaltyPointsTable.Points,
      CustomerId = LoyaltyDetailsTable.CustomerTable.CustomerId
    } into g
    select new TotalPointsUser{
      CustomerId = 
      g.Key.LoyaltyPointsId == 4 ? 
        (from RedeemPointsTable in dbpoints.RedeemPointsTable
        where
          RedeemPointsTable.CustomerId == 1
        select new {
          RedeemPointsTable.Amount
        }).Count(p => p.Amount != null) : g.Count(p => p.CustomerTable.CustomerId != null),
      Name=g.Key.Name,
      Points = 
      g.Key.LoyaltyPointsId == 4 ? (
        (from RedeemPointsTable in dbpoints.RedeemPointsTable
        where
         RedeemPointsTable.CustomerId == 1
        select new {
         RedeemPointsTable.Amount
        }).Sum(p => p.Amount) * g.Key.Points) : g.Sum(p => p.LoayaltyPointsTable.Points)
    }))
select new {
  CustomerId = t.Sum(p => p.CustomerId),        // here is the error
  Points= t.Sum(p => p.Points),                // and here
  Name = 
    ((from CustomerTable in dbpoints.CustomerTable
    where
      CustomerTable.CustomerId == 1
    select new {
      CustomerTable.Name
    }).First().Name)
}
           .ToList();
            return View(onetotal);
}

模型类:

public class TotalPointsUser
{
    public int CustomerId { get; set; }
    public string Name { get; set; }
    public int Points { get; set; }
}

我是 linq 的新手,我可以知道必须进行哪些更改吗?

蚂蚁帮助会很棒。

【问题讨论】:

  • Column1 = .Sum(p =&gt; p.TotalUserActions) 无效,使用它的别名
  • 我不明白这里的意思:Column1 = .Sum(p =&gt; p.TotalUserActions) 也许这是我不熟悉的语法? Sum 是什么对象的成员?这让我想起了 VB 中的With 语法,但在 C# 中从未见过。
  • 试试看:Column1 = g.Sum(p =&gt; p.TotalUserActions)
  • @Ehsan Sajjad,嗨,当我使用 g 时,我遇到了这个错误:当前上下文中不存在名称 'g'
  • @stom:我认为您在这里遗漏了一个基本点。类/属性名称很有用,但它们不是您要查找的变量的名称。在 C# 中,当您想要调用变量的实例方法(或引用变量的任何成员)时,您需要从引用变量开始。例如,如果变量名为t,那么您将调用t.Sum(),您不能只调用.Sum() 并期望编译器知道您在说什么。您可以尝试从更小的查询构建并使用更有意义的变量名来理解这一点。

标签: c# sql-server linq


【解决方案1】:

您的查询结构是:

from t in (
)
select new {
  Column1 = .Sum(p => p.TotalUserActions),        // here is the error
  Column2 = .Sum(p => p.AllTotalPoints),    // and here
}

您必须在结果中使用t 别名,如下所示:

from t in (
)
select new {
  Column1 = t.Sum(p => p.TotalUserActions),
  Column2 = t.Sum(p => p.AllTotalPoints),
}

但是您的结果查询中必须有这样的字段,现在不是这样。

我认为您应该用 SQL 编写查询并逐步将其传输到 LINQ。现在它非常令人困惑和产生错误。


更新
由于您的代码不包含此类文件,因此您遇到了错误。什么应该包含字段TotalUserActionsAllTotalPoints?尝试总结所有Points 字段,但不能说出TotalUserActions 是什么:

from t in (
)
select new {
  Column1 = t.Sum(p => p.TotalUserActions),
  Column2 = t.Sum(p => p.Points),
}

【讨论】:

  • ,当我使用“t”别名时:我收到此错误:TotalPointsUser 不包含“Sum”的定义,并且没有扩展方法“Sum”接受 TotalPointsUser 类型的第一个参数跨度>
  • ,我更改了预期结果查询的字段:'from t in () select new { CustomerId = t.Sum(p => p.CustomerId), Points = t.Sum(p => p.Points), }' ,但我仍然收到此错误:TotalPointsUser' 不包含 'Sum' 的定义,并且没有扩展方法 'Sum' 接受 TotalPointsUser' 类型的第一个参数,我得到单独的点但不能汇总子查询中的集合
  • @stom 更新了答案
  • 我已经删除了 TotalUserActions,现在我只想要 Total 所以我使用了,选择 new { Points = t.Sum(p => p.Points),但错误没有解决
  • this,有显示所有点的工作查询,this 图像显示它,我想总结所有集合 -
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多