如何有效地使用LINQ中的枢轴?
我搜索了如何使用linq进行枢轴数据并找到这个
我正在尝试将其应用于我的ODATA API服务。
表数据的复合密钥为{id,name,datatype}。
表分析具有键为ID。
因此,我尝试使用此代码,一切正常:
var pv = _context.Data.GroupBy(g => new { g.Id, g.Name, g.DataType })
.Select(s => new
{
Id = s.Key.Id,
Name = s.Key.Name,
DataType = s.Key.DataType,
C1 = s.Where(w => w.Component == "C1").Max(s => s.Value),
C2 = s.Where(w => w.Component == "C2").Max(s => s.Value),
C3 = s.Where(w => w.Component == "C3").Max(s => s.Value),
.
.
.
.
.
C98 = s.Where(w => w.Component == "C98").Max(s => s.Value),
C99 = s.Where(w => w.Component == "C99").Max(s => s.Value),
C100 = s.Where(w => w.Component == "C100").Max(s => s.Value)
});
var rs = pv.Where(w => w.Id == 1);
但是,当我尝试使用另一个表加入时,它会像永远运行:
var pv = _context.Data.GroupBy(g => new { g.Id, g.Name, g.DataType })
.Select(s => new
{
Id = s.Key.Id,
Name = s.Key.Name,
DataType = s.Key.DataType,
C1 = s.Where(w => w.Component == "C1").Max(s => s.Value),
C2 = s.Where(w => w.Component == "C2").Max(s => s.Value),
C3 = s.Where(w => w.Component == "C3").Max(s => s.Value),
.
.
.
.
.
C98 = s.Where(w => w.Component == "C98").Max(s => s.Value),
C99 = s.Where(w => w.Component == "C99").Max(s => s.Value),
C100 = s.Where(w => w.Component == "C100").Max(s => s.Value)
}).Join(_context.Analyses, p => p.Id, a => a.Id, (p, a) => new
{
Id = p.Id,
Name = p.Name,
DataType = p.DataType,
AnalysName = a.AnalysName,
C1 = p.C1,
.
.
C100 = p.C100,
});
var rs = pv.Where(w => w.Id== 1);
较少的聚合列(C1,C2 ...)查询速度越快。那么,我可以做这个吗?感谢您的查询或另一种方法的建议,将不胜感激,谢谢。
I searched for how to do pivot data with LINQ and found THIS
I am trying apply this for my OData API service.
Table Data has composite key as { Id, Name, DataType }.
Table Analyses has key as Id.
So, I try this code, and everything works fine:
var pv = _context.Data.GroupBy(g => new { g.Id, g.Name, g.DataType })
.Select(s => new
{
Id = s.Key.Id,
Name = s.Key.Name,
DataType = s.Key.DataType,
C1 = s.Where(w => w.Component == "C1").Max(s => s.Value),
C2 = s.Where(w => w.Component == "C2").Max(s => s.Value),
C3 = s.Where(w => w.Component == "C3").Max(s => s.Value),
.
.
.
.
.
C98 = s.Where(w => w.Component == "C98").Max(s => s.Value),
C99 = s.Where(w => w.Component == "C99").Max(s => s.Value),
C100 = s.Where(w => w.Component == "C100").Max(s => s.Value)
});
var rs = pv.Where(w => w.Id == 1);
But when I try to join with another table, it run like forever:
var pv = _context.Data.GroupBy(g => new { g.Id, g.Name, g.DataType })
.Select(s => new
{
Id = s.Key.Id,
Name = s.Key.Name,
DataType = s.Key.DataType,
C1 = s.Where(w => w.Component == "C1").Max(s => s.Value),
C2 = s.Where(w => w.Component == "C2").Max(s => s.Value),
C3 = s.Where(w => w.Component == "C3").Max(s => s.Value),
.
.
.
.
.
C98 = s.Where(w => w.Component == "C98").Max(s => s.Value),
C99 = s.Where(w => w.Component == "C99").Max(s => s.Value),
C100 = s.Where(w => w.Component == "C100").Max(s => s.Value)
}).Join(_context.Analyses, p => p.Id, a => a.Id, (p, a) => new
{
Id = p.Id,
Name = p.Name,
DataType = p.DataType,
AnalysName = a.AnalysName,
C1 = p.C1,
.
.
C100 = p.C100,
});
var rs = pv.Where(w => w.Id== 1);
The less aggregate column (C1, C2...) the faster the query. So, is there anyway I can make this? Any advice for the query or another way to approach this will be appreciated, thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论