Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.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# 将LINQ中表的月日格式转换为工作日格式_C#_Asp.net_Linq_Asp.net Mvc 5_Linq To Entities - Fatal编程技术网

C# 将LINQ中表的月日格式转换为工作日格式

C# 将LINQ中表的月日格式转换为工作日格式,c#,asp.net,linq,asp.net-mvc-5,linq-to-entities,C#,Asp.net,Linq,Asp.net Mvc 5,Linq To Entities,在我的程序中,我需要读取过去一周的给定值,按工作日对它们进行分组,然后将它们显示在MVC的表格中。我已经成功地显示了一长串数字天数(显示重复次数)及其匹配数据,但我需要的格式是从周一到周日,这意味着它必须组合在同一天发生的值 我的控制器: public ActionResult Index(string TimeState) { DateTime currentDate = DateTime.Now; DateTime today = DateTim

在我的程序中,我需要读取过去一周的给定值,按工作日对它们进行分组,然后将它们显示在MVC的表格中。我已经成功地显示了一长串数字天数(显示重复次数)及其匹配数据,但我需要的格式是从周一到周日,这意味着它必须组合在同一天发生的值

我的控制器:

  public ActionResult Index(string TimeState)
    {
        DateTime currentDate = DateTime.Now;

        DateTime today = DateTime.Today;
        DateTime week = DateTime.Today.AddDays(-7);
        string userID = User.Identity.GetUserId();
        string email = (from x in db.Users where x.Id == userID select x.Email).FirstOrDefault();



        IEnumerable<ValuesList> valuesList = from x in db.UsageDatas
                                             where week  <= x.Time && x.Time <= today && x.UserEmail == email                                                 
                                             select new ValuesList
                                             {
                                                 Dates = x.Time.Day.ToString(),
                                                 Values = x.Delta ,
                                             };


        State state = new State
        {
            state = TimeState,
            valuesLists = valueList,                                  
            selectedDate = "0"
        };
        return View(state);
    }
公共操作结果索引(字符串时间状态) { DateTime currentDate=DateTime.Now; DateTime today=DateTime.today; DateTime week=DateTime.Today.AddDays(-7); 字符串userID=User.Identity.GetUserId(); 字符串email=(从db.Users中的x开始,其中x.Id==userID选择x.email); IEnumerable valuesList=从x开始,以db.UsageDatas为单位 where week item.Values) } 型号:

   public class State
{

    public string state { get; set; }
    public IEnumerable<ValuesList> valuesLists { get;set;}
    public string TimeState { get; set; }
    public string selectedDate { get; set; }

}

public class ValuesList
{
  public string Dates { get; set; }
public string Values { get; set; }


}
公共类状态
{
公共字符串状态{get;set;}
public IEnumerable values列表{get;set;}
公共字符串时间状态{get;set;}
公共字符串selectedDate{get;set;}
}
公共阶级价值论者
{
公共字符串日期{get;set;}
公共字符串值{get;set;}
}
当前表的外观: 注意:它显示当前月份的日期*

我的LINQ非常有限,因此任何见解都将受到高度赞赏。
这是要添加今天的数据的新错误

? 如果有的话


你需要做一些分组和求和。我试过了,但我不知道如何以工作日格式发送到IEnumerable@NetMage只需将每天的增量相加。看起来不像
日期
应该是复数。在
select
组x.Delta by x.Time.DayOfWeek放入xg
之前,在
select
put
Dates=xg.Key.ToString(“ddd”)中,Values=xg.Sum()
您必须输入您试图得到的错误-如果编码正确,则没有意义。我想,有一个SelectMany缺失,谢谢,试图找出如何聚合增量现在我们都准备好了:)@JoshuaWeiss Replace
。选择…aggregate
with
。Sum(m=>m.Delta)
是的,这是一个字符串,我尝试将两个值转换为int,然后再转换回字符串,但我无法做到。添加对Delta`int.TryParse(m,out var value)的解析;`m显示错误:无法将~model.Usage date转换为字符串。我为所有的问题感到抱歉,但这个项目实际上是由于在3小时内,这是最后一点需要修复。所以我很绝望,哈哈。
   public class State
{

    public string state { get; set; }
    public IEnumerable<ValuesList> valuesLists { get;set;}
    public string TimeState { get; set; }
    public string selectedDate { get; set; }

}

public class ValuesList
{
  public string Dates { get; set; }
public string Values { get; set; }


}
        DateTime today = DateTime.Today;
        DateTime tomorrow = today.AddDays(1);
        DateTime week = today.AddDays(-7); // or -6  think about it
        string userID = User.Identity.GetUserId();
        string email = (from x in db.Users where x.Id == userID select x.Email).FirstOrDefault();

        var valuesList = db.UsageDatas
            .Where(x => x.Time >= week && x.Time < tomorrow && x.UserEmail == email)
            //.AsEnumerable() //posible need it for execute sql before take DayOfWeek
            .GroupBy(x => x.Time.DayOfWeek)
            .Select(x => new
            {
                DayOfWeek = x.Key,
                Values = x.Select(m => m.Delta).Aggregate((a, b) => a + "," + b)
            })
            .OrderBy(m => m.DayOfWeek)
            .Select(x => new ValuesList
            {
                Dates = x.DayOfWeek.ToString(),
                Values = x.Values
            });
 Values = x.Select(x =>
            {
                int.TryParse(x, out var value);
                return value;
            })
            .Sum()