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# Linq、join、group、count>;选择更多值_C#_Linq - Fatal编程技术网

C# Linq、join、group、count>;选择更多值

C# Linq、join、group、count>;选择更多值,c#,linq,C#,Linq,通过LINQ,我将执行以下操作来选择我的供应商及其产品数量: from s in context.Suppliers join p in context.Products on s equals p.Supplier group s by s.CompanyName into result select new { SupplierName = result.Key, ProductCount = result.Count() } 这很好,但我想从我的供应

通过LINQ,我将执行以下操作来选择我的供应商及其产品数量:

from s in context.Suppliers 
join p in context.Products on s equals p.Supplier 
group s by s.CompanyName 
into result 
select new {   
    SupplierName = result.Key,   
    ProductCount = result.Count() 
}
这很好,但我想从我的供应商表、供应商ID和供应商地址中选择更多属性,如:

....    
select new {   
    SupplierName = result.Key,   
    ProductCount = result.Count(),   
    SupplierId = ..,   
    SupplierAddress = ..,  
    }
有人知道怎么做吗


谢谢你的帮助

那么您确定所有具有相同
公司名称的
供应商
组都保证具有相同的
Id
地址

from s in context.Suppliers 
join p in context.Products on s equals p.Supplier 
group s by s.CompanyName 
into result 
select new {   
    SupplierName = result.Key,   
    ProductCount = result.Count(),
    SupplierId = result.First().Id,
    SuppliedAddress = result.First().Address 
}
如果您改为按
Id
分组,或者可能按以下所有方式分组,则看起来更自然:

from s in context.Suppliers 
join p in context.Products on s equals p.Supplier 
group s by new { s.CompanyName, s.Id, s.Address } 
into result 
select new {   
    ProductCount = result.Count(),
    SupplierName = result.Key.CompanyName,   
    SupplierId = result.Key.Id,
    SuppliedAddress = result.Key.Address 
}

因此,您确定所有具有相同
公司名称的
供应商
组都保证具有相同的
Id
地址

from s in context.Suppliers 
join p in context.Products on s equals p.Supplier 
group s by s.CompanyName 
into result 
select new {   
    SupplierName = result.Key,   
    ProductCount = result.Count(),
    SupplierId = result.First().Id,
    SuppliedAddress = result.First().Address 
}
如果您改为按
Id
分组,或者可能按以下所有方式分组,则看起来更自然:

from s in context.Suppliers 
join p in context.Products on s equals p.Supplier 
group s by new { s.CompanyName, s.Id, s.Address } 
into result 
select new {   
    ProductCount = result.Count(),
    SupplierName = result.Key.CompanyName,   
    SupplierId = result.Key.Id,
    SuppliedAddress = result.Key.Address 
}
编辑

嗯。。。除非我弄错了,否则这可以做得更干净:

context
    .Products
    .GroupBy(p=>p.Supplier)
    .Select(result=>new {
        SupplierName = result.Key,   
        ProductCount = result.Count(),
        SupplierId = result.Key.Id,   
        SupplierAddress = result.Key.Address,  
    }
连接来自DB中的FK关系,因此
产品
已经有了
供应商
。您似乎在自己的代码中发现了此设置(
…等于p.Supplier
),但未能理解其含义。抱歉从理解语法更改为方法链。它们对我来说更自然

作为对@Dan评论的补充(这对于Linq2Objects可能是正确的),在Linq2Sql中(我不能保证L2E是正确的,但我想它大致相同),如果您按FK关系生成的属性分组,则生成的SQL将按键值分组,而不是按整个实体分组。

EDIT

嗯。。。除非我弄错了,否则这可以做得更干净:

context
    .Products
    .GroupBy(p=>p.Supplier)
    .Select(result=>new {
        SupplierName = result.Key,   
        ProductCount = result.Count(),
        SupplierId = result.Key.Id,   
        SupplierAddress = result.Key.Address,  
    }
连接来自DB中的FK关系,因此
产品
已经有了
供应商
。您似乎在自己的代码中发现了此设置(
…等于p.Supplier
),但未能理解其含义。抱歉从理解语法更改为方法链。它们对我来说更自然


作为@Dan评论的补充(这对于Linq2Object可能是正确的),在Linq2Sql中(我不能保证L2E,但我想它大致相同),如果您按FK关系生成的属性分组,则生成的SQL将按键值分组,而不是按整个实体分组。

您的思路是正确的,只要核实两件事。。。您的输出类是否有存储SupplierId和SupplierAddress的字段?你只是粘贴了部分代码,我们能得到整行代码吗,包括LINQ?你在正确的轨道上,只需验证两件事。。。您的输出类是否有存储SupplierId和SupplierAddress的字段?您只粘贴了部分代码,我们可以得到整行代码,包括
result.FirstOrDefault().Id
result.FirstOrDefault().Address
中隐含的LINQ?,我同意Splenter的说法。如果要在不检查
null
的情况下使用该值,那么使用
或default
版本是没有意义的。@svick:你完全正确,出于某种原因,我认为LINQ中没有
First()
方法。现在已经很晚了,我想我需要睡一觉。是的,你完全正确,最好是按Id分组,但我只是打了一个简单的例子来澄清我的问题。第二个例子非常适合!谢谢
result.FirstOrDefault().Id
result.FirstOrDefault().Address
中隐含着空异常的可能性。我同意Splenter的观点。如果要在不检查
null
的情况下使用该值,那么使用
或default
版本是没有意义的。@svick:你完全正确,出于某种原因,我认为LINQ中没有
First()
方法。现在已经很晚了,我想我需要睡一觉。是的,你完全正确,最好是按Id分组,但我只是打了一个简单的例子来澄清我的问题。第二个例子非常适合!谢谢请注意,您还需要
Supplier
来实现
IEquatable
,这样才能起作用。@Dan,我扩展了我的答案以回应您的评论。请注意,您还需要
Supplier
来实现
IEquatable
,这样才能起作用。@Dan,我扩展了我的答案以回应您的评论。