LINQ查询-分组多个实体并求和

时间:2018-08-22 20:23:53

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

我正在尝试将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。

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

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

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

代替

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

然后将网络计算更新为

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)
                            };