【问题标题】:Get list of data from a column from a database, depending on the data in another column从数据库的列中获取数据列表,具体取决于另一列中的数据
【发布时间】:2020-06-07 13:10:45
【问题描述】:

描述:在控制器中,我从数据库(SQL Server / Entity Framework)下载加班列表并将其汇总为一个变量(现在可以使用)。

问题:我需要从“减号”列中选择数据库(列表)中的数据,但仅限从“下拉”列中选择选项“2”的地方。

并从我当前的总和中减去下载数据的总和。

控制器:

var userName = "MIPA";
var numberYear = 2020;
var numberMonth = 6;

// downloading the overtime list from each previous month from the database
var overtimeListVariable = _ecpContext.Card
                                      .Where(x => x.Login == userName && 
                                                  x.Year == numberYear && 
                                                  x.Month < numberMonth)
                                      .Select(b => string.IsNullOrEmpty(b.overtimeList) ? TimeSpan.Zero : TimeSpan.Parse(b.overtimeList))
                                      .ToList();

// Calculate sum
var sumOvertimeListVariable = overtimeListVariable.Aggregate(TimeSpan.Zero, (t1, t2) => t1 + t2);

SQL 服务器:

Id | Login | Year | Month | Day | Overtime |selectboxlist| Minus |
---+-------+------+-------+-----+----------+-------------+-------+
 1 | MIPA  | 2020 |   1   |  1  |   3:00   |      2      | 1:00  |
33 | MIPA  | 2020 |   1   | 30  |   1:00   |     null    | 1:30  |
44 | MIPA  | 2020 |   2   |  1  |   4:00   |      2      | 2:00  |
77 | MIPA  | 2020 |   2   | 30  |   2:00   |     null    | 2:30  |

查看:

@Html.DropDownListFor(m => m.Model1[nr_rows].DropdownClassList, new SelectList(Enum.GetValues(typeof(enumDropDown))), "  ", new { @class = "selectboxlist" })

@Html.TextBoxFor(m => m.Model1[nr_rows].overtimeList , new { @class = "overtime", @type = "time"})

@Html.TextBoxFor(m => m.Model1[nr_rows].minusList, new { @class = "minus", @type = "time"})

型号:

public partial class modelCard
{
    public string overtimeList { get; set; }
    public string minusList { get; set; }
    public enumDropDown? DropdownClassList{ get; set; }
}

public enum enumDropDown
{
    test1 = 1,
    test2 = 2,
    test3 =3
}

【问题讨论】:

    标签: c# asp.net sql-server entity-framework asp.net-core


    【解决方案1】:

    现在你已经成功获取了OvertimeList及其sum,你可以使用同样的方法获取Minus列的相关数据,只需添加如下where条件:

    var minusListVariable= _context.Card
                .Where(x => x.Login == userName && x.Year == numberYear &&
                       x.Month < numberMonth && x.selectboxlist=="2")
                .Select(b => string.IsNullOrEmpty(b.Minus) ? TimeSpan.Zero : TimeSpan.Parse(b.Minus))
                .ToList();
    
    var sumMinusListVariable = minusListVariable.Aggregate(TimeSpan.Zero, (t1, t2) => t1 + t2);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-13
      • 1970-01-01
      • 1970-01-01
      • 2013-05-06
      • 1970-01-01
      • 2013-10-16
      • 1970-01-01
      • 2019-03-17
      相关资源
      最近更新 更多