Home > OS >  Retrieve DateTimeOffset pairs from linq query
Retrieve DateTimeOffset pairs from linq query

Time:01-18

This must be logical equation to solve, but I can't understand the solution. I have an example that is working. However in real world scenario I am getting data from linq query. How to receive data from linq query to correspond the example?

Here is an example:

public List<GanttChartModel> GetActivites()
{
  var result = new List<GanttChartModel>();

  result.Add(new GanttChartModel { XAxisValue = "Design", YAxisValue = DateTimeOffset.Now.AddDays(-30) });
  result.Add(new GanttChartModel { XAxisValue = "Design", YAxisValue = DateTimeOffset.Now.AddDays(0) });

  result.Add(new GanttChartModel { XAxisValue = "Develop", YAxisValue = DateTimeOffset.Now.AddDays(-22) });
  result.Add(new GanttChartModel { XAxisValue = "Develop", YAxisValue = DateTimeOffset.Now.AddDays(-10) });

  result.Add(new GanttChartModel { XAxisValue = "Test", YAxisValue = DateTimeOffset.Now.AddDays(-14) });
  result.Add(new GanttChartModel { XAxisValue = "Test", YAxisValue = DateTimeOffset.Now.AddDays(-8) });

  return result;
}

Here is my current linq query that is not working:

  List<GanttChartModel> GanttChart = this.ProjectsList
  .Select(s => new GanttChartModel
  {
    XAxisValue = s.Name,
    YAxisValue = new DateTimeOffset(s.ProjectStartDate).AddDays((s.ProjectEndDate.Day - s.ProjectStartDate.Day)),
  }).ToList();

Here is model:

  public class GanttChartModel
  {
    public string XAxisValue { get; set; }
    public DateTimeOffset YAxisValue { get; set; }
  }

As you can see in example there are pairs of data between which gantt is built. How do I make linq query to output pairs of data where first is Start date and second is End date?

Project list example:

public List<Project> GetProjects()
{
  var result = new List<Project>();

  result.Add(new Project { ProjectId = Guid.NewGuid(), Name = "Project 1", ProjectStartDate = new DateTime(2022, 8, 15), ProjectEndDate = new DateTime(2022, 9, 15) });
  result.Add(new Project { ProjectId = Guid.NewGuid(), Name = "Project 2", ProjectStartDate = new DateTime(2022, 8, 22), ProjectEndDate = new DateTime(2022, 8, 25) });
  result.Add(new Project { ProjectId = Guid.NewGuid(), Name = "Project 3", ProjectStartDate = new DateTime(2022, 7, 25), ProjectEndDate = new DateTime(2022, 8, 2) });

  return result;
}

Project model:

  public class Project
  {
    public Guid ProjectId { get; set }
    public string Name { get; set; }
    public DateTime ProjectStartDate { get; set; }
    public DateTime ProjectEndDate { get; set; }
  }

This is by the way for Apex charts in Blazor, however question is about how to build a pairs output in linq.

  <ApexChart TItem="GanttChartModel"
           Options=Options
           XAxisType="XAxisType.Datetime">
    <ApexRangeSeries TItem="GanttChartModel"
                   Items="this.GanttChart"
                   XValue="@(e => e.XAxisValue)"
                   YValue="@(e => e.YAxisValue.ToUnixTimeMilliseconds())" />
  </ApexChart>

CodePudding user response:

I think you can try to use ForEach to make the unpivot.

var result = new List<GanttChartModel>();
 this.ProjectsList.ForEach(r =>{
 result.Add(new GanttChartModel
            {
                XAxisValue = r.Name,
                YAxisValue = r.ProjectStartDate
            });
 result.Add(new GanttChartModel
            {
                XAxisValue = r.Name,
                YAxisValue = r.ProjectEndDate
            });
});

https://dotnetfiddle.net/1Dl8hm

CodePudding user response:

You can use Enumerable.SelectMany to return multiple results, eg:

var results = this.ProjectsList
                  .SelectMany(s => new []{
                                      new GanttChartModel{
                                      {
                                          XAxisValue = s.Name,
                                          YAxisValue = s.ProjectStartDate
                                      },
                                      new GanttChartModel{
                                      {
                                          XAxisValue = s.Name,
                                          YAxisValue = s.ProjectEndDate
                                      }
                              });

results is an IEnumerable<GanttChartModel>. You can convert it to an array, list or dictionary using normal LINQ methods like ToList() or ToArray().

Be careful though - casting a DateTime whose DateTimeKind isn't UTC results in a local offset. If you want to treat the DateTime values as UTC you'll have to specify the offset explicitly:

var utc=TimeSpan.Zero;

var results = this.ProjectsList
                  .SelectMany(s => new []{
                      new GanttChartModel{
                      {
                          XAxisValue = s.Name,
                          YAxisValue =new DateTimeOffset( s.ProjectStartDate,utc);
                      },
                      new GanttChartModel{
                      {
                          XAxisValue = s.Name,
                          YAxisValue =new DateTimeOffset(s.ProjectEndDate, utc)
                      }
                    });
  •  Tags:  
  • Related