如何有效地使用LINQ中的枢轴?

发布于 2025-01-24 13:31:49 字数 2525 浏览 0 评论 0原文

我搜索了如何使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文