Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 按两个标准(日期和车辆编号)对数据库记录进行分组计数,并在索引页中显示错误_C#_Asp.net Mvc_Asp.net Mvc 5 - Fatal编程技术网

C# 按两个标准(日期和车辆编号)对数据库记录进行分组计数,并在索引页中显示错误

C# 按两个标准(日期和车辆编号)对数据库记录进行分组计数,并在索引页中显示错误,c#,asp.net-mvc,asp.net-mvc-5,C#,Asp.net Mvc,Asp.net Mvc 5,我是asp.NETMVC新手。我希望任何人都能在这方面帮助我 这是一个维护多辆车燃油费用记录的程序。 如果一个月内某一特定车辆的燃油输入记录超过5条,我想创建一条消息。例如,当“2020年6月”一个月内车辆的燃油输入记录计数超过5时,应在索引页或主页上显示错误消息。我一直在寻找这个,但没有运气 模型、控制器和视图中的相关位如下所示 车辆列表型号: namespace VehicleReg2.Models { public class VehicleList { [Key]

我是asp.NETMVC新手。我希望任何人都能在这方面帮助我

这是一个维护多辆车燃油费用记录的程序。 如果一个月内某一特定车辆的燃油输入记录超过5条,我想创建一条消息。例如,当“2020年6月”一个月内车辆的燃油输入记录计数超过5时,应在索引页或主页上显示错误消息。我一直在寻找这个,但没有运气

模型、控制器和视图中的相关位如下所示

车辆列表型号:

namespace VehicleReg2.Models
{
    public class VehicleList
    {   [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public byte Id { get; set; }
        public string VType { get; set; }
        public string RegNumber { get; set; }
        public string ModelMake { get; set; }
}}
namespace VehicleReg2.Models
{
    public class Fuel
    {
        public int Id { get; set; }

        [DataType(DataType.Currency)]
        public decimal FAmount { get; set; }

        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MMM/yyyy}")]
        public DateTime FDate { get; set; }

        public VehicleList VehicleList { get; set; }
        public byte VehicleListId { get; set; }

        public double FQty { get; set; }     
    }}
public ViewResult Index()
        {var fuel = _context.Fuels.Include(c => c.VehicleList).ToList();
         return View(fuel);
        }
燃油型号:

namespace VehicleReg2.Models
{
    public class VehicleList
    {   [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public byte Id { get; set; }
        public string VType { get; set; }
        public string RegNumber { get; set; }
        public string ModelMake { get; set; }
}}
namespace VehicleReg2.Models
{
    public class Fuel
    {
        public int Id { get; set; }

        [DataType(DataType.Currency)]
        public decimal FAmount { get; set; }

        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MMM/yyyy}")]
        public DateTime FDate { get; set; }

        public VehicleList VehicleList { get; set; }
        public byte VehicleListId { get; set; }

        public double FQty { get; set; }     
    }}
public ViewResult Index()
        {var fuel = _context.Fuels.Include(c => c.VehicleList).ToList();
         return View(fuel);
        }
燃油控制器索引:

namespace VehicleReg2.Models
{
    public class VehicleList
    {   [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public byte Id { get; set; }
        public string VType { get; set; }
        public string RegNumber { get; set; }
        public string ModelMake { get; set; }
}}
namespace VehicleReg2.Models
{
    public class Fuel
    {
        public int Id { get; set; }

        [DataType(DataType.Currency)]
        public decimal FAmount { get; set; }

        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MMM/yyyy}")]
        public DateTime FDate { get; set; }

        public VehicleList VehicleList { get; set; }
        public byte VehicleListId { get; set; }

        public double FQty { get; set; }     
    }}
public ViewResult Index()
        {var fuel = _context.Fuels.Include(c => c.VehicleList).ToList();
         return View(fuel);
        }

您必须按
日期
分组,然后进行
计数
控制。我没有尝试下面的查询,但它应该与您想要的类似

db.Fuels.GroupBy(x => x.FDate.Date)
            .Where(x => x.ToList().VehicleList.RegNumber.Count() > 5)
            .ToDictionary(x => x.Key, x => x.ToList());
下面的查询查找每个
月份
特定
车辆
的所有
Fuel
条目(超过5个)

db.Fuels.GroupBy(x => new { x.FDate.Date, x.VehicleListId })
            .Where(x => x.Count() > 5)
            .ToDictionary(x => x.Key, x => x.ToList());

您可以搜索计数吗?LinqI确实尝试了Linq,但在使用单月条件时未成功。你能给我推荐一些有助于我的案例的链接吗?我明天有时间的时候会尝试研究一下,但要注意以下几点:1。您应该使用ViewBag来存储查询结果。2.count lambda表达式类似于
db.Fuels.Where(x=>x.VehicleList.RegNumber.count()>5)
@JacobHallgarth我曾尝试过类似的表达式,但在我的例子中需要涉及每月5次计数条件(“FDate”)。对于每个月,如果条目超过5个计数,则应在索引视图中显示一个简单错误。很抱歉,它似乎以错误“LINQ to Entities中不支持指定的类型成员'Date'。有什么建议吗?请尝试
db.Fuels.GroupBy(x=>new{EntityFunctions.TruncateTime(x.FDate),x.VehicleListId}).Where(x=>x.Count()>5).ToDictionary(x=>x.Key,x=>x.ToList())您好,谢谢您的即时回复。我确实使用了DbFunctions.TruncateTime,但得到的错误值不能为null。参数名称:sourceI使用您的代码在视图部分进行了少量修改,获得了完美的输出。是否可以在controller
@foreach(Model.GroupBy(x=>new{Month=x.FDate.Month,Year=x.FDate.Year,Vehicle=x.VehicleList.RegNumber})中实现它。其中(x=>x.Count()>5)。ToDictionary(x=>x.Key,x=>x.ToList()){很高兴听到这个消息!