【发布时间】: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