c# – Linq匿名类型成员必须在Sub Query中声明
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – Linq匿名类型成员必须在Sub Query中声明,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2593字,纯文字阅读大概需要4分钟。
内容图文
我正在尝试将this SQL查询转换为Linq,以在asp MVC中的View中返回,
Linq的预期产出:
TotalPoints Name
231 John
我在第二个选择新的子查询中得到此错误:
Invalid expression term ‘.’
Invalid anonymous type member declarator. Anonymous type members must
be declared with a member assignment, simple name or member access.
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的新手,我可以知道必须做出哪些改变?
Ant帮助会很棒.
解决方法:
您的查询结构是:
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.现在它非常令人困惑并且产生错误.
更新:
你有错误,因为你的代码不包含这样的文件.什么应该包含字段TotalUserActions和AllTotalPoints?尝试对所有Points字段求和,但不能说TotalPserActions是什么:
from t in (
)
select new {
Column1 = t.Sum(p => p.TotalUserActions),
Column2 = t.Sum(p => p.Points),
}
内容总结
以上是互联网集市为您收集整理的c# – Linq匿名类型成员必须在Sub Query中声明全部内容,希望文章能够帮你解决c# – Linq匿名类型成员必须在Sub Query中声明所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。