【问题标题】:C# - Database context cast returnC# - 数据库上下文转换返回
【发布时间】:2019-06-13 18:07:15
【问题描述】:

我目前正在使用 Asp.net 数据库上下文创建 API,但遇到了问题。我有两个数据库表,我想输出一些数据,但 不是全部

我的桌子:

Employee Table:
+----+------+---------------+------------+---------------+
| Id | Name |     Mail      |   Phone    |   Birthday    |
+----+------+---------------+------------+---------------+
|  1 | John | John@Mail.com | 987 72 123 | 25-July  2000 |
|  2 | Stan | Stan@Mail.com | 978 21 342 | 10-April 1998 |
|  3 | Kim  | Kim@Mail.com  | 973 28 199 | 26-March 2001 |
+----+------+---------------+------------+---------------+

Shift Table:
+------------+--------------------+--------------------+--------+
| EmployeeId |       Start        |        End         | Active |
+------------+--------------------+--------------------+--------+
|     1      | 10-June 2019 08:00 | 10-June 2019 16:00 |  true  |
|     2      | 10-June 2019 12:00 | 10-June 2019 18:00 |  true  |
|     3      | 10-June 2019 16:00 | 11-June 2019 00:00 |  true  |
|     2      | 11-June 2019 08:00 | 11-June 2019 16:00 |  true  |
+------------+--------------------+--------------------+--------+

在我的表中,Employee Table 和 Shift Table 在 Employee.Id 和 Shift.Id 之间存在一对多关系

使用我创建的控制器,我已经能够返回所有数据,但是我想返回一个特定的数据集(不是所有的) 基本上我想要返回的只是


+----+------+--------------------+--------------------+--------+
| Id | Name |       Start        |        End         | Active |
+----+------+--------------------+--------------------+--------+
|  1 | John | 10-June 2019 08:00 | 10-June 2019 16:00 | true   |
|  2 | Stan | 10-June 2019 12:00 | 10-June 2019 18:00 | true   |
|  3 | Kim  | 10-June 2019 16:00 | 11-June 2019 00:00 | true   |
|  2 | Stan | 11-June 2019 08:00 | 11-June 2019 16:00 | true   |
+----+------+--------------------+--------------------+--------+


我在Controller中尝试了以下代码,但我似乎无法返回我想要的数据集

private readonly DatabaseContext context;

public ManageUsersController(DatabaseContext Dbcontext)
{
    context = Dbcontext;
}

[HttpGet("Users")]
public List<Employee> GetUsers()
{
    var data = context.Employee.Include(c=> c.Schedule).toList();
    return data;
}

我检索数据的模型是这样的

public partial class Employee
    {
        public int Id { get; set; }
        public int PhoneNr { get; set; }
        public string Name { get; set; }
        public string Mail { get; set; }
        public DateTime Birthday { get; set; }
        public bool Active { get; set; }

        //Relationship
        public ICollection<Schedule> Schedule { get; set; }
    }

    public partial class Schedule
    {
        public int Id { get; set; } //EmployeeId
        public DateTime Start { get; set; }
        public DateTime End{ get; set; }
        public bool? Active { get; set; }

        //Relationship
        public Employee Employee { get; set; }
    }

最后我的 DatabaseContext 文件如下所示:

public class DatabaseContext: DbContext
{
    public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options)
    {
    }

    public DbSet<Schedule> Schedule { get; set; }
    public DbSet<Employee> Employee { get; set; }

}

希望我的问题不会太长,提前感谢您的回复

【问题讨论】:

  • 所以您是说要过滤“Shift”表的数据?如果是,您要添加哪种过滤器...日期、名称或?
  • 您需要过滤的数据是基于任何 where 条件。您可以在包含功能后使用。
  • 我想将这些表组合成一个表,就像您在 SQL 中使用以下代码所做的那样:“选择 e.ID, e.Name, s.Start, s.End, s .Active 内连接 Schedule s on s.Id = e.Id From Employee e "
  • @dimmik 谢谢!一直在寻找这样的东西至少 2 天了!!

标签: c# asp.net database api dbcontext


【解决方案1】:

创建另一个只包含必要属性的类。

public class EmployeeWithScheduleModel
{
    public int Id {get;set;}
    public string Name {get;set;}
    public DateTime Start {get;set;}
    public DateTime End {get;set;}
    public bool? Active {get;set;}
}

然后修改你的控制器方法以便只返回必要的:

public List<EmployeeWithScheduleModel> GetUsers()
{
    var data = context.Employee.Include(c=>c.Schedule).ToList();

    var response = new List<EmployeeWithScheduleModel>();

    foreach (var item in data)
    {
        if(!item.Schedule.Any() || item.Schedule.Count > 1) continue; //your custom logic here

       var schedule = item.Schedule.FirstOrDefault();


       var employeeWithSchedule = new EmployeeWithScheduleModel
       {
          Id = item.Id,
          Name = item.Name,
          Start = schedule.Start,
          End = schedule.End,
          Active = schedule.Active
       };

       response.Add(employeeWithSchedule);
    }     

    return response;
}

【讨论】:

    【解决方案2】:

    正如@dimmik 在问题的评论中回答的那样,可以使用 Linq 返回数据在我的情况下,这将是:

    [HttpGet("")]
    public List<EmployeeTable> GetUsers()
    {
        var d = from e in context.Employee
            join s in context.Schedule on e.Id equals s.EmployeeId
            select new
            {
                Id = e.Id,
                Name = e.Name,
                Phone = e.PhoneNr,
                Mail = e.Mail,
                State = e.Active
            };
        return (List<EmployeeTable>)(Object)d.ToList();
    }
    

    EmployeeTable,是我创建的一个自定义类,具有 Id、Name、Phone、Mail 和 State 作为属性

    【讨论】:

      【解决方案3】:
      [HttpGet("Users")]
      public List<EmployeeTable> GetUsers()
      {
      var empDetails=context.Employee.join(context.Schedule,e=>e.Id,d=>d.Id,(e,d)=>new {
                  Id = e.Id,
                  Name = e.Name,
                  Start = d.Start,
                  End = d.End,
                  Active = d.Active
      return (List<EmployeeTable>)(Object)empDetails.ToList();
      });
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-11-29
        • 2014-03-19
        • 2020-02-16
        • 1970-01-01
        • 2020-09-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多