【问题标题】:LINQ Query - Group multiple entities and sumLINQ 查询 - 对多个实体进行分组并求和
【发布时间】:2018-08-22 20:23:53
【问题描述】:

我正在尝试将 SQL 查询转换为 LINQ

以下是相关模型:

public class GISTracts 
{ 
    public int? LeaseID     { get; set; } 
    public int? TractID     { get; set; } 
    public string County    { get; set; } 
    public string Source    { get; set; } 
    public string Legal     { get; set; } 
    public decimal? Net     { get; set; } 
    public string TractName { get; set; } 
    public decimal? GrossAc { get; set; } 
    public decimal? GasExecutive { get; set; } 
}

这是我的 sql 查询:

select leaseId, Tracts.Id as TractsId, Tracts.Name as TractsName, Tracts.GrossAc, County, Source,  District, Legal,
Sum(Interests.GasExecutive*Tracts.GrossAc) as Net
from WorkingInterestGroups
Inner Join Interests on WorkingInterestGroups.Id = Interests.WorkingInterestGroupId
Inner Join Tracts on Interests.TractId = Tracts.Id
group by leaseId, Tracts.Id , Tracts.Name, Tracts.GrossAc, County, District, Legal, Source;

这是我的 LINQ 查询

var myResults = from WIG in db.WorkingInterestGroups
                join In in db.Interests on WIG.Id equals In.WorkingInterestGroupId
                join Tr in db.Tracts on In.TractId equals Tr.Id
                where WIG.LeaseId == LeaseID
                group WIG by new
                {
                    WIG.LeaseId,
                    Tr.Id,
                    Tr.Name,
                    Tr.GrossAc,
                    Tr.County,
                    Tr.District,
                    Tr.Legal,
                    Tr.Source                                
                } into gcs
                select new GISTracts
                {
                    LeaseID = gcs.Key.LeaseId,
                    TractID = gcs.Key.Id,
                    TractName = gcs.Key.Name,
                    GrossAc = gcs.Key.GrossAc,
                    County = gcs.Key.County,
                    Source = gcs.Key.Source,
                    Legal = gcs.Key.Legal,
                    Net = gcs.Sum(x=>x.GrossAcres * x.Interests.GasExecutive)
                };

无法在我的 LINQ 查询中的 select 语句中获取 Net Field。

非常感谢任何帮助。

【问题讨论】:

  • "无法在我的 LINQ 查询中的 select 语句中获取网络字段" 但它就在那里,不是吗?您的意思是发生了一些错误还是没有得到预期的结果?
  • 您的代码似乎正确,能否添加您的模型以便我们了解更多详情。
  • public class GISTracts { public int? LeaseID { get; set; } public int? TractID { get; set; } public string TractName { get; set; } public decimal? GrossAc { get; set; } public string County { get; set; } public string Source { get; set; } public string Legal { get; set; } public decimal? GasExecutive { get; set; } public decimal? Net { get; set; } }
  • 如果我使用 Net = gcs.Sum(x => x.GrossAcres * x.Interests.Sum(j=>j.GasExecutive)),错误就会消失。但是Net和SQL查询结果不一样
  • "错误消失" -- 哪个错误?您在此处所做的事情在 Stack Overflow 问题中更常见。你说出你想要什么和你尝试了什么,很好,但显然它没有成功,你忘记了为什么。

标签: c# linq linq-to-sql sum


【解决方案1】:

根本原因是,您没有在group 子句中包含Tracts 实体。尝试 将group by 子句更新为 -

group new { WIG,In,Tr } by new {....} into gcs

而不是

group WIG by new {....} into gcs

然后将 Net 计算更新为

Net = gcs.Sum(x=>x.Tr.GrossAcres * x.In.GasExecutive) //Not sure if GrossAcres part of tracks or other entity, update it accordingly.

您的linq 查询将如下所示。

var myResults = from WIG in db.WorkingInterestGroups
                            join In in db.Interests on WIG.Id equals In.WorkingInterestGroupId
                            join Tr in db.Tracts on In.TractId equals Tr.Id
                            where WIG.LeaseId == LeaseID
                            group new { WIG,In,Tr } by new
                            {
                                WIG.LeaseId,
                                Tr.Id,
                                Tr.Name,
                                Tr.GrossAc,
                                Tr.County,
                                Tr.District,
                                Tr.Legal,
                                Tr.Source                                
                            } into gcs
                            select new GISTracts
                            {
                                LeaseID = gcs.Key.LeaseId,
                                TractID = gcs.Key.Id,
                                TractName = gcs.Key.Name,
                                GrossAc = gcs.Key.GrossAc,
                                County = gcs.Key.County,
                                Source = gcs.Key.Source,
                                Legal = gcs.Key.Legal,
                                Net = gcs.Sum(x=>x.Tr.GrossAcres * x.In.GasExecutive)
                            };

【讨论】:

    猜你喜欢
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-01
    相关资源
    最近更新 更多