Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用join、group by和where将sql转换为linq_C#_Linq_Join_Group By_Aggregate - Fatal编程技术网

C# 使用join、group by和where将sql转换为linq

C# 使用join、group by和where将sql转换为linq,c#,linq,join,group-by,aggregate,C#,Linq,Join,Group By,Aggregate,我有以下sql,我想将其转换为linq SELECT Contrato.finca, SUM(Pago.Importe_subtotal) FROM Pago, Contrato WHERE Pago.Contrato = Contrato.ID AND Pago.pagado = 1 GROUP BY Contrato.finca ORDER BY 2 DESC GO 我现在在linq中有以下内容,但是group by不起作用 var x = from contrato in ctx.Con

我有以下sql,我想将其转换为linq

SELECT Contrato.finca, SUM(Pago.Importe_subtotal)
FROM Pago, Contrato
WHERE Pago.Contrato = Contrato.ID AND Pago.pagado = 1
GROUP BY Contrato.finca
ORDER BY 2 DESC
GO
我现在在linq中有以下内容,但是group by不起作用

var x = from contrato in ctx.Contratos 
        join pago in ctx.Pagos
        on contrato.ID equals pago.Contrato 
        where pago.pagado == true 
        group contrato by contrato.finca
        select contrato.Finca1;
试一试


我认为这应该有效:

ctx.Pagos.Where(m=>m.pagado==true).GroupBy(m=>m.Contrato.finca) 
    .Select(m=> new { Id= m.Key, Val= m.Sum(n => n.Importe_subtotal)})

我得到以下错误:错误1'System.Linq.IGrouping'不包含'Finca1'的定义,也不包含扩展方法'Finca1'-g在intelligence中不显示属性您在代码Finca1中的属性,这就是我放置Finca1的原因,但我也看到您有finca。我不知道你的模型类型,我只是根据我在你的代码中看到的来做的。但在最初的代码中,您使用了into语句。我还添加了总和聚合,因为在初始查询中有它。现在我得到了预期的错误标识符;'“new”是一个关键字。Finca1是由外键约束自动生成的。finca是fincaI的id的名称。请修复该问题,然后重试。唯一的问题是我不确定是Finca1还是Finca,因为我在您的查询中看到您有Finca,但在LINQ中您有Finca1。仍然得到错误:预期标识符;'“new”是一个关键字,我添加了一个“)”来结束总额。这种语法对我来说是全新的。你有关于如何使用它的链接吗?我得到以下错误:无法从用法推断方法“System.Linq.Enumerable.Join(System.Collections.Generic.IEnumerable,System.Collections.Generic.IEnumerable,System.Func,System.Func,System.Func)”的类型参数。尝试显式指定类型参数。这是方法语法。我认为它更容易阅读:使用Linq时是否需要连接?如果实体模型设置正确,则相关实体应为属性。您的数据库架构是什么样子的?不起作用。。。什么是不起作用的?它是一个财产。它在实体模型的Finca1Is Pago中是Contrato的一个属性?你能发布一个数据库模型的图表,这样我就可以看到这些项是如何关联的吗?
ctx.Pagos.Where(m=>m.pagado==true).GroupBy(m=>m.Contrato.finca) 
    .Select(m=> new { Id= m.Key, Val= m.Sum(n => n.Importe_subtotal)})