11

我正在尝试计算 LINQ 中特定州的员工人数。

我有这样的事情:

States
     |
     Cities
          |
          Posts
              |
              Employees

我怎样才能Employees通过选中State的手来计数?

我的实体是:

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; }
}

编辑:有趣的是我可以Posts毫无问题地计数,但是当我想尝试@HamletHakobyan 帖子中的方式时,我得到了NullReferenceException,我不知道为什么?

4

8 回答 8

12

我不确定,但试试这个:

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();
于 2013-01-22T14:09:33.987 回答
5

您可以为此使用 LINQ to EntitiesSumCount方法:

int count = db.States
  .Where( state => state.StateId == X )
  .Sum( state => state.Cities
    .Sum( city => city.Posts
      .Sum( post.Employees.Count() )
    )
  )
;
于 2013-01-22T14:12:53.340 回答
2
var q = from emp in dc.Employees
    from post in dc.Posts
    from city in dc.Cities
    from state in dc.States
    where emp.PostID == post.ID
    && post.CityID == city.ID
    && city.StateID == state.ID
    && state.Name == "Tehran"
    select emp;

或者干脆

var q = from emp in dc.Employees
    where emp.Post.City.State.Name == "Tehran"

q是那个州的雇员名单,现在你可以很容易地计算他们(我相信你知道如何计算他们)。

于 2013-01-22T14:06:15.587 回答
1

如果我正确地理解了你,这将是我对你的问题的解决方案,因为我无法找到将 4 个 Linq 粘在一起的方法,但也许你可以做到这一点

顺便说一句,如果您需要可选的,请查看此链接

        var list = new List<Province>();

        string sProvinceName = "";
        string sCityName = "";
        string sPostName = "";
        string sEmployeeFirstName = "";
        string sEmployeeSureName = "";


        var iListListcitys = from province in list
                             where province != null
                             where province.Cities != null
                             where province.ProvinceName == sProvinceName
                             select province.Cities;

        var iListListposts = from icitys in iListListcitys
                             from city in icitys
                             where city != null
                             where city.ElectricPosts != null
                             where city.CityName == sCityName
                             select city.ElectricPosts;

        var iListListEmployees = from posts in iListListposts
                                 from post in posts
                                 where post != null
                                 where post.Employees != null
                                 where post.PostName == sPostName
                                 select post.Employees;

        var listEmployees = from employees in iListListEmployees
                            from employee in employees
                            where employee != null
                            where employee.FirstName == sEmployeeFirstName || employee.SureName == sEmployeeSureName
                            select employee;

        var count = listEmployees.Count();
于 2013-02-08T15:33:24.667 回答
1

我有完全相同的(问题)问题,但不幸的是我在互联网上找到的其他人的解决方案是错误的!所以我试图解决这个问题。所以,假设我们有一些国家,每个国家都有一些州,每个州都有一些城市,我们想要获得一些国家视图模型,这些模型具有每个国家的 Id 和名称以及所有州和所有城市的计数属于每个国家。解决方案如下:

var varResult =
    OurDatabaseContext.Countries
    .Select(current => new {
        Id = current.Id,
        Name = current.Name,
        StateCount = current.States.Count,
        CityCount = current.States.Count == 0 ? 0 :
        current.States.Select(state =>
          new { CityCount = state.Cities.Count }).Sum(q => q.CityCount)
    };
于 2016-04-20T11:31:56.547 回答
0
db.States.Where(p => p.yourcondition == whatever).Cities.Posts.Employees.Count()
于 2013-01-22T14:06:17.820 回答
0

试试这个:

Province province = ...;
int count = province.Cities.Include(c => c.Posts.Select(p => p.Employees))
    .Where(c => c != null)
    .SelectMany(c => c.Posts)
    .Where(p => p != null)
    .SelectMany(p => p.Employees)
    .Where(e => e != null)
    .Count();

然后将.Where子句一一去掉,找出问题所在。

您还可以使用joins:

int count = (from c in province.Cities
             join p in db.Posts on c equals p.City
             join e in db.Employees on p equals e.ElectricPost
             select e).Count();
于 2013-02-10T20:56:53.917 回答
0
var cites = (from s in _db.Province where s.ProvinceName == 'name' select s.Cities);
int count = (from p in _db.Posts
         where city.Contains(p.City)
         select p.Employees).Count();
于 2013-02-11T06:10:13.477 回答