gpt4 book ai didi

c# - 获取嵌套实体的数量

转载 作者:可可西里 更新时间:2023-11-01 08:38:47 26 4
gpt4 key购买 nike

我正在尝试获取 LINQ 中特定州的员工数。

我有这样的东西:

States
|
Cities
|
Posts
|
Employees

如何通过手头选定的 State 获取 Employees 计数?

我的实体是:

public class Province : EntityBase
{
public String ProvinceName { get; set; }
public virtual IList<City> Cities { get; set; }
}

public class City : EntityBase
{
public String CityName { get; set; }

public virtual Province Province { get; set; }
public virtual IList<Post> ElectricPosts { get; set; }
}

public class Post : EntityBase
{
public String PostName { get; set; }
public virtual City City { get; set; }
public virtual IList<Employee> Employees { get; set; }
}

public class Employee : Person
{
public virtual String FirstName { get; set; }
public virtual String SureName { get; set; }
public virtual Post ElectricPost { get; set; }
}

编辑: 有趣的是,我可以毫无问题和异常地计算帖子,但是当我想尝试@HamletHakobyan 帖子中的方法时,我收到 NullReferenceException 并且我不知道为什么?

最佳答案

我不确定,但试试这个:

var state = ((IObjectContextAdapter)context)
.ObjectContext
.CreateObjectSet<Province>("States") as ObjectQuery<Province>;
// States is entitySetName

int count = state.Cities.Include("ElectricPosts.Employees")
.SelectMany(c => c.Posts)
.SelectMany(p => p.Employees)
.Count();

关于c# - 获取嵌套实体的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14460372/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com