C# 如何按某个属性对一个表进行分组,然后对LINQ中其他表(带有外键)中的元素进行计数?

C# 如何按某个属性对一个表进行分组,然后对LINQ中其他表(带有外键)中的元素进行计数?,c#,linq,C#,Linq,假设我有两个表(实体): 我想按“城市”属性对所有人员实体进行分组,并计算每个城市在JoinTable中引用的人员数量。 我如何在LINQ中查询这个问题?我不太确定,您想获得什么。但我认为是这样的: // Example Data (would be great if you could write some in your questions..) List<Person> ps = new List<Person>() { new Person() { Id

假设我有两个表(实体):

我想按“城市”属性对所有人员实体进行分组,并计算每个城市在JoinTable中引用的人员数量。
我如何在LINQ中查询这个问题?

我不太确定,您想获得什么。但我认为是这样的:

// Example Data (would be great if you could write some in your questions..)
List<Person> ps = new List<Person>()
{
    new Person() { Id = 1, City = "Cologne" },
    new Person() { Id = 2, City = "Cologne" },
    new Person() { Id = 3, City = "Berlin" },
    new Person() { Id = 4, City = "Berlin" },
};
List<JoinTable> join = new List<JoinTable>()
{
    new JoinTable() { Id = 1, Person_Id = 1, SomeOther_Id = 1000 },
    new JoinTable() { Id = 1, Person_Id = 1, SomeOther_Id = 2000 },
    new JoinTable() { Id = 1, Person_Id = 2, SomeOther_Id = 1000 },
    new JoinTable() { Id = 1, Person_Id = 2, SomeOther_Id = 2000 },
    new JoinTable() { Id = 1, Person_Id = 3, SomeOther_Id = 3000 },
    new JoinTable() { Id = 1, Person_Id = 3, SomeOther_Id = 4000 },
};

// Join the Table and select a new object.
var tmp = ps.Join(
    join, // which table/list should be joined
    o => o.Id, // key of outer list
    i => i.Person_Id, // key of inner list
    (o, i) => new { City = o.City, Id = i.Id, SomeOtherId = i.SomeOther_Id}); // form a new object with three properties..

// now we can group out new objects
var groupingByCity = tmp.GroupBy(g => g.City);

// let's see what we got..
foreach (var g in groupingByCity)
{
    Console.WriteLine(g.Key + ": " + g.Count());

    foreach (var g2 in g.GroupBy(a => a.SomeOtherId)) // And yes we can group out values again..
    {
        Console.WriteLine("    " + g2.Key + ": " + g2.Count());
    }
}
//示例数据(如果您能在问题中写一些就好了……)
List ps=新列表()
{
new Person(){Id=1,City=“Cologne”},
新人(){Id=2,City=“科隆”},
新人(){Id=3,City=“Berlin”},
newperson(){Id=4,City=“Berlin”},
};
列表联接=新列表()
{
新的JoinTable(){Id=1,Person_Id=1,SomeOther_Id=1000},
新的JoinTable(){Id=1,Person_Id=1,SomeOther_Id=2000},
新的JoinTable(){Id=1,Person_Id=2,SomeOther_Id=1000},
新的JoinTable(){Id=1,Person_Id=2,SomeOther_Id=2000},
新的JoinTable(){Id=1,Person_Id=3,SomeOther_Id=3000},
新的JoinTable(){Id=1,Person_Id=3,SomeOther_Id=4000},
};
//联接表并选择一个新对象。
var tmp=ps.Join(
join,//应该联接哪个表/列表
o=>o.Id,//外部列表的键
i=>i.Person\u Id,//内部列表的键
(o,i)=>new{City=o.City,Id=i.Id,SomeOtherId=i.SomeOther_Id});//形成一个具有三个属性的新对象。。
//现在我们可以将新对象分组
var groupingByCity=tmp.GroupBy(g=>g.City);
//让我们看看我们得到了什么。。
foreach(groupingByCity中的var g)
{
Console.WriteLine(g.Key+:“+g.Count());
foreach(g.GroupBy中的var g2(a=>a.SomeOtherId))//是的,我们可以再次分组值。。
{
Console.WriteLine(“+g2.Key+”:“+g2.Count());
}
}
// Example Data (would be great if you could write some in your questions..)
List<Person> ps = new List<Person>()
{
    new Person() { Id = 1, City = "Cologne" },
    new Person() { Id = 2, City = "Cologne" },
    new Person() { Id = 3, City = "Berlin" },
    new Person() { Id = 4, City = "Berlin" },
};
List<JoinTable> join = new List<JoinTable>()
{
    new JoinTable() { Id = 1, Person_Id = 1, SomeOther_Id = 1000 },
    new JoinTable() { Id = 1, Person_Id = 1, SomeOther_Id = 2000 },
    new JoinTable() { Id = 1, Person_Id = 2, SomeOther_Id = 1000 },
    new JoinTable() { Id = 1, Person_Id = 2, SomeOther_Id = 2000 },
    new JoinTable() { Id = 1, Person_Id = 3, SomeOther_Id = 3000 },
    new JoinTable() { Id = 1, Person_Id = 3, SomeOther_Id = 4000 },
};

// Join the Table and select a new object.
var tmp = ps.Join(
    join, // which table/list should be joined
    o => o.Id, // key of outer list
    i => i.Person_Id, // key of inner list
    (o, i) => new { City = o.City, Id = i.Id, SomeOtherId = i.SomeOther_Id}); // form a new object with three properties..

// now we can group out new objects
var groupingByCity = tmp.GroupBy(g => g.City);

// let's see what we got..
foreach (var g in groupingByCity)
{
    Console.WriteLine(g.Key + ": " + g.Count());

    foreach (var g2 in g.GroupBy(a => a.SomeOtherId)) // And yes we can group out values again..
    {
        Console.WriteLine("    " + g2.Key + ": " + g2.Count());
    }
}