C# 集成多个group by查询(将一个表与其他各种表连接起来并计算匹配项)

C# 集成多个group by查询(将一个表与其他各种表连接起来并计算匹配项),c#,linq-to-sql,group-by,C#,Linq To Sql,Group By,在应用程序中,我需要显示每个状态(从db.Geodata)的客户、产品的数量(计数){insert other entity here}(从db.Custmers,db.products),等等) 结果可能是这样的 所以我写了一个查询 from customer in db.Kunden join pin in db.Geodata on customer.AdressNr equals pin.AdressNr group customer by pin.Status into gro

在应用程序中,我需要显示每个状态(从
db.Geodata
)的客户、产品的数量(计数){insert other entity here}(从
db.Custmers
db.products
),等等)

结果可能是这样的

所以我写了一个查询

from customer in db.Kunden
join pin in db.Geodata
    on customer.AdressNr equals pin.AdressNr
group customer by pin.Status
into groups
select new {Status = groups.Key, Count = groups.Count()};
还有一个

from product in db.Products
join pin in db.Geodata
    on product.AdressNr equals pin.AdressNr
group product by pin.Status
into groups
select new {Status = groups.Key, Count = groups.Count()};
还有一些。它们很好用。但我正在努力将它们集成到一个查询中(从而整合重复的部分,即最后两行)

我怎样才能做到这一点

编辑:有人能帮我改进这个问题吗?是否缺少任何关键信息?这是太本地化了,还是太宽泛了?

即使现在已经太迟了,也可能会对您有所帮助

LINQ并没有被设计成像SQL那样构建它的语句,在SQL中,您可能会使用
StringBuilder
或类似的东西

LINQ优于SQL的优点是在编码时进行编译时检查。在SQL中,您必须运行代码并进行试错

然而,我发现了一个类似的问题,在您的问题提出5个月后,该问题已被询问/回答:

采用这种方法,您可能会遇到如下情况:

public object[] GetCustomers()
{
    return GetEntityCount("Customers");
}

private object[] GetEntityCount(string entityName)
{
    return (from obj in GetSpecificEntity(entityName) join pin ...).ToArray();
}

private IEnumerable GetSpecificEntity(string entityName)
{
    switch (entityName)
    {
        case "Customers": return db.Customers;
    }
}
public object[] GetCustomers()
{
    return GetEntityCount("Customers");
}

private object[] GetEntityCount(string entityName)
{
    return (from obj in GetSpecificEntity(entityName) join pin ...).ToArray();
}

private IEnumerable GetSpecificEntity(string entityName)
{
    switch (entityName)
    {
        case "Customers": return db.Customers;
    }
}