在 Entity Framework 中写入查询
Write query in Entity Framework
我有一个查询,我在 SQL Server Management Studio 中 运行 成功,其中 return 是屏幕截图中显示的 table 值
我使用的查询是:
SELECT tcoid, COUNT(*) ownleasetank
FROM TankProfile
WHERE ownleasetank = 3
GROUP BY tcoid
现在我正在使用 Entity Framework 来简化我的示例项目。
我使用此方法将 return 的 table 值作为数组对象:
public async Task<Object> GetLeaseInformationPrincipal()
{
ISOTMSEntities context = new ISOTMSEntities();
var testleaseinfo = from d in context.TankProfiles
join f in context.TankOperators
on d.tcoid equals f.tcoId
where (d.ownleasetank == 3)
select new { f.tcoName, d.ownleasetank } into x
group x by new { x.tcoName } into g
select new
{
tconame = g.Key.tcoName,
ownleasetank = g.Select(x => x.ownleasetank).Count()
};
return testleaseinfo.ToList();
}
但它无法正常工作。我还尝试了其他方法,当我在 Entity Framework 中使用 where 和 groupby 方法时,它对我来说无法正常工作。
有人知道这个的解决方案吗?
var c = from t in context.TankProfile
where t.ownleasetank == 3
group t by t.tcoid into g
select new { tcoid=g.Key, ownleasetank=g.Select(x => x.ownleasetank).Count() };
return c.ToList();
使用 LINQ 方法非常简单:
context.TankProfiles
.Where(t => t.ownleasetank = 3)
.GroupBy(t => t.tcoid)
.Select(g => new {g.Key, g.Count()})
.ToArray();
我不知道为什么在您的 C# 版本的查询中有这样的操作 join
,而您的 SQL 查询非常简单。你必须重新考虑:)
我有一个查询,我在 SQL Server Management Studio 中 运行 成功,其中 return 是屏幕截图中显示的 table 值
我使用的查询是:
SELECT tcoid, COUNT(*) ownleasetank
FROM TankProfile
WHERE ownleasetank = 3
GROUP BY tcoid
现在我正在使用 Entity Framework 来简化我的示例项目。
我使用此方法将 return 的 table 值作为数组对象:
public async Task<Object> GetLeaseInformationPrincipal()
{
ISOTMSEntities context = new ISOTMSEntities();
var testleaseinfo = from d in context.TankProfiles
join f in context.TankOperators
on d.tcoid equals f.tcoId
where (d.ownleasetank == 3)
select new { f.tcoName, d.ownleasetank } into x
group x by new { x.tcoName } into g
select new
{
tconame = g.Key.tcoName,
ownleasetank = g.Select(x => x.ownleasetank).Count()
};
return testleaseinfo.ToList();
}
但它无法正常工作。我还尝试了其他方法,当我在 Entity Framework 中使用 where 和 groupby 方法时,它对我来说无法正常工作。
有人知道这个的解决方案吗?
var c = from t in context.TankProfile
where t.ownleasetank == 3
group t by t.tcoid into g
select new { tcoid=g.Key, ownleasetank=g.Select(x => x.ownleasetank).Count() };
return c.ToList();
使用 LINQ 方法非常简单:
context.TankProfiles
.Where(t => t.ownleasetank = 3)
.GroupBy(t => t.tcoid)
.Select(g => new {g.Key, g.Count()})
.ToArray();
我不知道为什么在您的 C# 版本的查询中有这样的操作 join
,而您的 SQL 查询非常简单。你必须重新考虑:)